我正在做一些android测试,但是在模拟方法时遇到问题。
我有这个用例:
private val getData: GetData = mock()
测试是:
@Test
fun exampleTest() {
whenever(getData()).thenReturn(Single.just(<myDATA>))
mActivityTestRule.launchActivity(Intent())
....
}
而GetData
是:
class GetData constructor(
private val repository: DataRepository
) {
operator fun invoke() = repository.getData()
}
问题是当我在执行whenever(getData()).thenReturn(Single.just(<myDATA>)
时,invoke
被调用并且存储库显然为空,然后我的测试崩溃了。
这是堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'io.reactivex.Single ....DataRepository.getData()' on a null object reference
at ....GetData.invoke(GetProducts.kt:13)
at ....MainActivityTest.exampleTest(MainActivityTest.kt:66)
at ....MainActivityTest.exampleTest(MainActivityTest.kt:52)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Tests ran to completion.
我正在单元测试中(在test
文件夹下)进行相同的工作,但是我无法理解为什么androidTest中会发生这种情况。
我也尝试将invoke()
替换为其他方法,并且仍在发生:
class GetData constructor(
private val repository: DataRepository
) {
fun test() = repository.getData()
}
当我打电话给whenever(getData.test()).thenReturn(Single.just(<myDATA>))
时同样失败
您有任何建议吗?
谢谢