使用 SparseIntArray 和其他来自 android.util 的本地单元测试。*
Local Unit Tests with SparseIntArray and others from android.util.*
Android Studio 建议我使用 SparseIntArray
而不是标准 Java 中的 Hashmap<Integer, Integer>
。
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 中使用任何包含此 class 的方法。我总是以 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
只有 returns 所有调用的默认值。我知道我可以使用 Instrumented Unit Tests 但它会减慢测试过程。
如果您想测试 class 平台依赖性,您应该使用 Robolectric 框架:http://robolectric.org/
Android Studio 建议我使用 SparseIntArray
而不是标准 Java 中的 Hashmap<Integer, Integer>
。
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 中使用任何包含此 class 的方法。我总是以 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
只有 returns 所有调用的默认值。我知道我可以使用 Instrumented Unit Tests 但它会减慢测试过程。
如果您想测试 class 平台依赖性,您应该使用 Robolectric 框架:http://robolectric.org/