我正在对我的视图模型进行单元测试,我总是不断得到NullPointerException
。
这是我的视图模型代码 -
class LoginViewModel(private val myUseCase: MyUseCase) :BaseViewModel() {
private val viewState = LoginViewState()
fun onLoginClicked() =
Transformations.map(
myUseCase.performUseCaseAction(
MyAction.LoginUser(
email,password)
)
) {
when (it) {
is MyResult.Loading -> viewState.copy(loading = true)
is MyResult.UserLoggedIn -> viewState.copy(
loading = false,
userLoggedIn = true
)
is MyResult.Error -> viewState.copy(loading = false, error = it.error)
}
}
}
这是MyUseCase接口代码 -
interface MyUseCase {
fun performUseCaseAction(action: MyAction): LiveData<MyResult>
}
这是相同的单元测试 -
@RunWith(MockitoJUnitRunner::class)
class LoginViewModelTest {
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
private lateinit var viewModel: LoginViewModel
@Mock
private lateinit var myUseCase: MyUseCase
@Mock
private lateinit var observer: Observer<LoginViewState>
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
viewModel = LoginViewModel(useCase)
}
@Test
fun login_loginClicked_userLoggedInExpected() {
//Given
val viewState = LoginViewState()
//When
val liveData1 = MutableLiveData<MyResult>()
`when`(useCase.performUseCaseAction(
MyAction.LoginUser("email","password")
)).thenReturn(liveData1)
liveData1.postValue(MyResult.UserLoggedIn)
viewModel.onLoginClicked().observeForever(observer)
//Then
verify(observer).onChanged(viewState.copy(loading = true))
verify(observer).onChanged(viewState.copy(loading = false, userLoggedIn = true))
}
}
这是我得到的输出 -
java.lang.NullPointerException
at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at androidx.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:437)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:232)
at com.client.personaldiary.view.viewmodel.LoginViewModelTest.onLoginClicked(LoginViewModelTest.kt:88)
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.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
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 org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
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 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)
似乎关于存根(何时/然后返回(计时问题。 对于大多数顺序语句来说,这不是问题, 但是在存根实时数据时,这很棘手,因为
当您的视图模型和模拟的MyUseCase设置好时, 模拟的MyUseCase已经被评估过了。 但是,在测试方法中,
`when`(useCase.performUseCaseAction(
MyAction.LoginUser("email","password")
)).thenReturn(liveData1)
以下方法不受上述存根方法的影响,
viewModel.onLoginClicked()
所以返回 null,因此"观察永远"提高 NPE。
我思维的良好做法是 1. 视图模型的初始化是在存根之后
`when`()...
viewModel = LoginViewModel(useCase)
或在源代码中使用 liveData 协程生成器,请参阅: https://github.com/android/architecture-components-samples/tree/master/LiveDataSample
liveData { emitSource(....map { ... }) }