我正在尝试使用Spek测试改造API
它将在{...} 块上的上引发nullPointerException
关联的stacktrace:https://pastebin.com/gy6dLtGg
这是我的考试课@RunWith(JUnitPlatform::class)
class AccountCheckViewModelTest : Spek({
include(RxSchedulersOverrideRule)
val httpException = mock<HttpException> {
on { code() }.thenReturn(400)
}
given(" account check view model") {
var accountCheckRequest = mock<CheckExistingAccountRequest>()
var accountCheckResponse = mock<CheckExistingAccountResponse>()
var webService = mock<IAPICalls>()
val accountCheckViewModel = spy(VMAccountCheck(webService))
beforeEachTest {
accountCheckRequest = mock<CheckExistingAccountRequest>() {
on { email }.thenReturn("foo@mail")
}
accountCheckResponse = mock<CheckExistingAccountResponse>() {
on { firstName }.thenReturn("foo")
on { email }.thenReturn("foo@mail")
}
webService = mock<IAPICalls> {
on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
}
}
on("api success") {
accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)
it("should call live data with first name as foo") {
verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
}
}
}
}
这是我的RxSchedulersOverrideSpek类 class RxSchedulersOverrideSpek : Spek({
beforeGroup {
RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
}
})
最佳答案
您应该使用memoized
正确设置测试值。问题在于accountCheckViewModel
是在Spek的发现阶段初始化的,传递给webService
的accountCheckViewModel
模拟是此时的值(您没有模拟其任何方法)。 beforeEachTest
在执行阶段运行,您已在此处将webService
重新分配为适当的模拟,但accountCheckViewModel
仍保留先前的值。
given(" account check view model") {
val accountCheckRequest by memoized {
mock<CheckExistingAccountRequest>() {
on { email }.thenReturn("foo@mail")
}
}
val accountCheckResponse by memoized {
mock<CheckExistingAccountResponse>() {
on { firstName }.thenReturn("foo")
on { email }.thenReturn("foo@mail")
}
}
val webService by memoized {
mock<IAPICalls> {
on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
}
}
val accountCheckViewModel by memoized {
spy(VMAccountCheck(webService))
}
on("api success") {
accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)
it("should call live data with first name as foo") {
verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
}
}
}