我不知道为什么会发生此错误。我需要测试是否成功调用了数据库接口。我尝试了几个小时都没有成功。
课程
class VisitUseCase(private var webService: WebService, private var databaseInterface: SupervisorDatabaseInterface) : VisitUseCaseInterface {
fun getVisitsFromApi(): Single<List<VisitsItem>> {
Log.e("API", "API")
return databaseInterface.getUserToken()
.flatMap { it -> webService.getVisits("Token ${it.token}", "future", "true") }
.map { it -> it.visits!! }
.doOnSuccess { it -> databaseInterface.insertListOfVisits(it) }
}
databaseInterface.getUserToken(“”,“”,“”)->返回单个-UserToken-
webService.getVisits()-> return Single-RoutineVisit-
地图-> ruturn列表-访问-
测试
class VisitUseCaseTest {
private val immediateScheduler = object : Scheduler() {
override fun createWorker(): Worker {
return ExecutorScheduler.ExecutorWorker(Executor { it.run() })
}
}
var webService: WebService = Mockito.mock(WebService::class.java)
var databaseInterface: SupervisorDatabaseInterface = Mockito.mock(SupervisorDatabaseInterface::class.java)
var mVisitUseCase: VisitUseCase = VisitUseCase(webService, databaseInterface)
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
RxJavaPlugins.setInitIoSchedulerHandler { immediateScheduler }
RxAndroidPlugins.setInitMainThreadSchedulerHandler {
immediateScheduler }
RxJavaPlugins.setComputationSchedulerHandler { immediateScheduler
}
}
@After
fun tearDown() {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
@Test
fun `success get from api`() {
val listSingleVisit = Single.just(listOf<VisitsItem>())
val userTokenSingle = Single.just(UserToken())
val routineVisitsSingle = Single.just(RoutineVisits())
`when`(webService.getVisits("", "", "")).thenReturn(routineVisitsSingle)
`when`(databaseInterface.getUserToken()).thenReturn(userTokenSingle)
mVisitUseCase.getVisitsFromApi().test().assertNoErrors()
verify(databaseInterface,
times(1)).insertListOfVisits(listSingleVisit.blockingGet())
}
}
令人开心的事
java.lang.AssertionError: Error(s) present: [java.lang.NullPointerException: The single returned by the mapper is null] (latch = 0, values = 0, errors = 1, completions = 0)
at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:191)
at io.reactivex.observers.BaseTestConsumer.assertNoErrors(BaseTestConsumer.java:276)
at test.id.domain.visitUseCase.VisitUseCaseTest.success get from api(VisitUseCaseTest.kt:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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 org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException: The single returned by the mapper is null
at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
at
io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback
.onSuccess(SingleFlatMap.java:76)
at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.internal.operators.single.SingleDoOnSuccess.subscribeActual(SingleDoOnSuccess.java:35)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.Single.test(Single.java:3534)
... 25 more
答案 0 :(得分:1)
您的模拟设置不是您想要的:
`when`(webService.getVisits("", "", "")).thenReturn(routineVisitsSingle)
仅当用空字符串调用routineVisitsSingle
时,才会返回webService.getVisits
,但是您的代码将其调用为:
webService.getVisits("Token ${it.token}", "future", "true")
您的模拟应该使用类似这样的设置:
`when`(webService.getVisits("Token ", "future", "true")).thenReturn(routineVisitsSingle)
由您(以及代码的逻辑)决定是否要完全模拟这些值,还是要使用any()
或其他Matchers