java - 使用 SparseIntArray 和其他来自 android.util.* 的本地单元测试

标签 java android unit-testing android-gradle-plugin android-testing

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/

相关文章:

java - 协助菜鸟进行Java堆应用

java - 从 map 获取我的 DFS 遍历的 getNode - Java

android,如何以编程方式设置单选按钮按钮的左填充?

java - Okhttp.Builder w/sslSocketFactory 中的 ExceptionInInitializerError

android - 需要帮助读取 Android 的 SQLite 数据库

java - 如何在不使用 PowerMock 中的 PrepareFor 注释的情况下模拟私有(private)方法?

java - Java/Scala 的 ConfigFactory 在 C# 中的等价物是什么?

unit-testing - 使用 NUnit 进行 MEF 和单元测试

unit-testing - 单元测试和有序测试 - 最佳实践

java - 我们可以在 oozie 决策节点中使用参数吗?