Android Studio 推荐我使用 SparseIntArray
而不是 Hashmap<Integer, Integer>
来自标准 Java。
SparseIntArray
map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping.
我认为它确实适合我的代码,但我不能在 Local Unit Tests 中使用包含此类的任何方法.我总是以 RuntimeException 结束...
java.lang.RuntimeException: Method put in android.util.SparseIntArray not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.util.SparseIntArray.put(SparseIntArray.java)
...
有没有办法导入这个方法进行本地测试?我不认为它使用任何 Android 的功能。 unitTests.returnDefaultValues = true
只返回所有调用的默认值。我知道我可以使用 Instrumented Unit Tests但它会减慢测试过程。
最佳答案
如果你想测试具有平台依赖性的类,你应该使用 Robolectric 框架:http://robolectric.org/
关于java - 使用 SparseIntArray 和其他来自 android.util.* 的本地单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848195/