简体   繁体   中英

Unit testing viewmodel in android using mockito MVVM pattern

This is my repository class

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)
}

}

This is my Viewmodel

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()
}

}

I want to write unit test cases for my code. I have written a sample test class here

 @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)

}
}

Now i need some clarification here what happens is that when the parameters are "sdemo" && "sdemo" 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

This is the log i get when i change sdemo to 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)

I was able to test ViewModel with following

@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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM