预期结果
在一个名为ExampleUnitTest.kt的本地JUnit测试类中导入库,如androidx.test:core:1.2.0
、androidx.test.ext:truth:1.2.0
、com.google.truth:truth:0.44
等。
观察到结果
错误信息
未解决的引用: test
实现方式
ExampleUnitTest.kt
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.google.common.truth.Truth.assertThat
androidTest/
)还是单元测试(test/
)?看起来你已经导入正确的包,但是也许你的Gradle依赖指令与测试类所在位置不匹配。 - CommonsWareInstrumentationRegistry.getInstrumentation().getTargetContext()
或getApplicationContext()
,我收到错误消息_java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation._。这是使用库import androidx.test.platform.app.InstrumentationRegistry
和import androidx.test.core.app.ApplicationProvider.getApplicationContext
。 - AdamHurwitzApplicationProvider
是用于仪器化测试的,至少目前是这样。我认为计划将其与基于 Mockito 的模拟一起统一用于单元测试,作为氮项目的一部分,但我认为这还没有准备好。 - CommonsWareInstrumentationRegistry.getInstrumentation().getTargetContext()
怎么样?我读到这应该在JUnit测试中用于访问app上下文这里。 - AdamHurwitz