First time unit testing, and using Mockito. I'm not sure if I'm thinking about testing this correctly. Here is the situation:
In my Android app, I am using Model-View-Presenter. I am trying to test a method in my presenter class called validateCredential(serviceManager: ServiceManager, email: String, password: String)
to see if the service manager I pass to it will eventually call a callback (this method is called by the view) by verifying it with mockito.
// method in presenter class
override fun validateCredential(serviceManager: ServiceManager, email: String, password: String) {
loginModel = LoginModel(email, password)
serviceManager.getParent(email, password)
serviceManager.execute()
}
// callback implemented by presenter class
private fun handleLoginResult(result: ServiceManager.RequestResult, data: ByteArray, responseCode: Int, optionalParam: String) {
...
mView.startHomeScreen()
}
The presenter class also implements a callback interface (IServiceAsyncTaskCallback
) that is provided to the serviceManager
's constructor. What I want in this specific unit test is to verify that mView.startHomeScreen()
is called.
The problem:
- Android unit testing seems to require that ServiceManager be mocked (ServiceManager extends abstract class AsyncTask) because when I call
execute()
, the Android unit testing lib will throw an exception if it is not mocked. - However, if I mock the ServiceManager, I can't provide it with two necessary parameters to the constructor, which SHOULD be mocked, if I'm understanding unit testing correctly. The two parameters to the constructor are an interface callback (which is the presenter class), and a class object that is responsible for sending JSON through http. Both of these should be mocked, correct? Because in a unit test you don't want these dependencies to actually make HTTP calls or call callbacks, right?
- It seems I'm overthinking this. What I really want is to see if my view object which is passed to presenter, calls
startHomeScreen()
, so I should really just forget testing thevalidateCredentialMethod()
and just callhandleLoginResult(...)
directly. Is this better than the above route? - However, another problem is that even if I call
handleLoginResult(...)
directly to test if the mock view that is passed to presenter is called, that method code contains a call to JSONObject which is Android related code, and since it belongs to the android.jar file, it will throw an exception because it is not mocked! Am I supposed to provide an injection for that too?!
I'm lost as how to test this. What's the correct way to verify that the mock view has called startHomeScreen()
?