【问题标题】:Unit testing viewmodel in android using mockito MVVM pattern使用 mockito MVVM 模式在 android 中对视图模型进行单元测试
【发布时间】:2018-12-05 10:00:51
【问题描述】:

这是我的存储库类

class UserRepository (private val apiInterface: ApiInterface, private val utils: Utils) {

fun makeLoginOnServer(userName: String , password : String): Single<UserResponse>? {
    val hasConnection = utils.isConnectedToInternet()
    var observableFromApi: Single<UserResponse>? = null
    if (hasConnection) {
        observableFromApi = loginUser(userName,password)
    }
    return if (hasConnection)
        observableFromApi!!
    else
        null
}

private fun loginUser(userName: String , password : String): Single<UserResponse> {
    return apiInterface.makeLoginOnServer(userName,password)
}

}

这是我的视图模型

class LoginViewModel(private val userRepository: UserRepository) : ViewModel() {

var userResult: MutableLiveData<UserResponse> = MutableLiveData()
var userError: MutableLiveData<String> = MutableLiveData()
var userLoader: MutableLiveData<Boolean> = MutableLiveData()
private lateinit var disposableObserver: DisposableSingleObserver<UserResponse>

fun userResult(): LiveData<UserResponse> {
    return userResult
}

fun userError(): LiveData<String> {
    return userError
}

fun userLoader(): LiveData<Boolean> {
    return userLoader
}

fun makeLoginOnServer(userName: String , password: String) {
    disposableObserver = object : DisposableSingleObserver<UserResponse>() {
        override fun onSuccess(t: UserResponse) {
            userResult.postValue(t)
            userLoader.postValue(false)
        }
        override fun onError(e: Throwable) {
            userError.postValue(Constants.getErrorMessage(e))
            userLoader.postValue(false)
        }
    }
    userRepository.makeLoginOnServer(userName, password)
            ?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(disposableObserver)
}

fun disposeElements() {
    if (!disposableObserver.isDisposed) disposableObserver.dispose()
}

}

我想为我的代码编写单元测试用例。 我在这里写了一个示例测试类

 @RunWith(MockitoJUnitRunner::class)
 class GithubActivityViewModelTest {

@Rule
@JvmField
var rule = InstantTaskExecutorRule()

companion object {
    @ClassRule
    @JvmField
    val schedulers = RxImmediateSchedulerRule()
}

@Mock
lateinit var userRepository: UserRepository

@Mock
lateinit var observer: Observer<UserResponse>

lateinit var loginViewModel: LoginViewModel


@Before
fun setUp() {
    loginViewModel = LoginViewModel(userRepository)
}

@Test
fun shouldShowGithubAccountLoginName() {
    val githubAccount = UserResponse()
    Mockito.`when`(userRepository.makeLoginOnServer("sdemo","sdemo"))
        .thenReturn(Single.just(githubAccount))
    loginViewModel.userResult.observeForever(observer)
    loginViewModel.makeLoginOnServer("sdemo","sdemo")
    assert(loginViewModel.userResult.value == githubAccount)

}
}

现在我需要澄清一下,当参数为"sdemo" &amp;&amp; "sdemo" 时,我获得了案例的测试通过,但如果我在一个地方更改任何单个值,我会得到一个空指针异常并且我的测试失败。 我想知道如何用 mockito 断言该异常。 请帮忙

这是我将 sdemo 更改为 sdem 时得到的日志

java.lang.AssertionError: Assertion failed

at com.connect.viewmodels.GithubActivityViewModelTest.shouldShowGithubAccountLoginName(LoginViewModelTest.kt:54)
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.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 com.connect.RxImmediateSchedulerRule$apply$1.evaluate(RxImmediateSchedulerRule.kt:39)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
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)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

【问题讨论】:

  • 你也可以分享你的日志吗
  • 我已分享完毕
  • 我没有看到 NullPointerExceptio。由于断言而失败。所以,我猜,当你告诉模拟只有在它是sdem/sdem 时才返回一个响应。当你填充不同的参数时,它不会返回任何东西并且失败,或者返回 null 并且你得到你的 NPE。你能澄清一些问题吗?)

标签: android unit-testing junit kotlin mockito


【解决方案1】:

我能够通过以下方式测试 ViewModel

@RunWith(MockitoJUnitRunner.class)
public class AutoConfigureViewModelTest {

   private final String TAG = this.getClass().getSimpleName();

   @Mock
   private Application application;

   @Rule
   public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

   private AutoConfigureViewModel viewModel;

   @Before
   public void setup() throws Exception {
       /* test actual method */
       viewModel = Mockito.spy(new AutoConfigureViewModel(application));

   }

   @Test
   public void getTestStringForEMH() {
       /* this test is to make sure CRC function works as expected for EMH */
       byte[] test = viewModel.getTestStringForDevice("EMH");
       byte[] exp = {95, 81, 48, 48, 48, 49, 50, 1, -93, 13 };

       String strEMH = new String(test);
       String strExp = new String(exp);
       assertEquals(strEMH, strExp);
   }


   @After
   public void tearDown() throws Exception {
       viewModel = null;
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2013-08-20
    • 2011-05-13
    • 2017-11-22
    • 1970-01-01
    • 2014-05-17
    相关资源
    最近更新 更多