问题
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