title: Unit testing viewmodel in android using mockito MVVM pattern
body: <p>This is my repository class </p>
  
  <pre><code>class UserRepository (private val apiInterface: ApiInterface, private val utils: Utils) {
  
  fun makeLoginOnServer(userName: String , password : String): Single&lt;UserResponse&gt;? {
      val hasConnection = utils.isConnectedToInternet()
      var observableFromApi: Single&lt;UserResponse&gt;? = null
      if (hasConnection) {
          observableFromApi = loginUser(userName,password)
      }
      return if (hasConnection)
          observableFromApi!!
      else
          null
  }
  
  private fun loginUser(userName: String , password : String): Single&lt;UserResponse&gt; {
      return apiInterface.makeLoginOnServer(userName,password)
  }
  
  }
  </code></pre>
  
  <p>This is my Viewmodel</p>
  
  <pre><code>class LoginViewModel(private val userRepository: UserRepository) : ViewModel() {
  
  var userResult: MutableLiveData&lt;UserResponse&gt; = MutableLiveData()
  var userError: MutableLiveData&lt;String&gt; = MutableLiveData()
  var userLoader: MutableLiveData&lt;Boolean&gt; = MutableLiveData()
  private lateinit var disposableObserver: DisposableSingleObserver&lt;UserResponse&gt;
  
  fun userResult(): LiveData&lt;UserResponse&gt; {
      return userResult
  }
  
  fun userError(): LiveData&lt;String&gt; {
      return userError
  }
  
  fun userLoader(): LiveData&lt;Boolean&gt; {
      return userLoader
  }
  
  fun makeLoginOnServer(userName: String , password: String) {
      disposableObserver = object : DisposableSingleObserver&lt;UserResponse&gt;() {
          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()
  }
  
  }
  </code></pre>
  
  <p>I want to write unit test cases for my code.
  I have written a sample test class here</p>
  
  <pre><code> @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&lt;UserResponse&gt;
  
  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)
  
  }
  }
  </code></pre>
  
  <p>Now i need some clarification here what happens is that when the parameters are <code>"sdemo" &amp;&amp; "sdemo"</code> i get test pass for the cases but if i change any single value at one place i get a null pointer exception and my test fails.
  I want to know how to assert that exception with mockito.
  Please help</p>
  
  <p>This is the log i get when i change sdemo to sdem</p>
  
  <pre><code>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)
  </code></pre>