Unit testing ArrayMap throws Method put in android.util.ArrayMap not mocked

跟風遠走 提交于 2021-02-10 16:42:29

问题


I am trying to unit test a method that uses array map and I keep getting the following exception:

java.lang.RuntimeException: Method put in android.util.ArrayMap not mocked. See http://g.co/androidstudio/not-mocked for details.

code:

  ArrayMap<String, String> sampleMap =new ArrayMap<>();
                            for (SampleInfo info : details.getSampleList())
                            {
                                sampleMap.put(info.getkey(),info.getName());
                            }

I have tried:

testOptions {
        unitTests.returnDefaultValues = true
    }

This way the exception is avoided but the map is always empty. How to resolve this issue?


回答1:


Worked for me, make sure its in the correct part of the build.gradle

android {

  ...

  testOptions { 
     unitTests.returnDefaultValues = true
  }
}



回答2:


As @ShadowFlame stated on above comment I used array map support "androidx.collection.ArrayMap" instead of "android.util.ArrayMap" and it works fine.




回答3:


You can try because it is ArrayMap using key value/pair

        ArrayList<ArrayMap<String, String>> listArray2 = new ArrayList<>();
        for (SampleInfo info1 : details.getSampleList())
        {
            sampleMap.put("keyname",info1.getName());

            listArray2.add(sampleMap);

        }
        Log.e("****sadflkj **",listArray2.toString());


来源:https://stackoverflow.com/questions/45124522/unit-testing-arraymap-throws-method-put-in-android-util-arraymap-not-mocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!