This is the first time I use mockito & roboelectric to make unit test on android studio.
I want to verify that view.onSuccessLogin() called when request to server / API is success. But, I got an error "wanted but not invoked - However, there was exactly 1 interaction with this mock"
Here is my presenter class :
class PresenterLogin internal constructor(private val view: View) {
private val compositeDisposable = CompositeDisposable()
interface View : BaseView {
fun onSuccessLogin()
}
fun requestLogin(username: String, password: String) {
view.showProgressDialog()
val disposable = ApiHelper.service.requestSignIn(ApiHeaders().headers,
username, password, AppPreference.getInstance().firebaseToken)
.map { response -> return@map response.toString() }
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
{ response ->
view.hideProgressDialog()
view.onSuccessLogin()
},
{ error ->
view.hideProgressDialog()
view.showError(error.message)
}
)
compositeDisposable.add(disposable)
}
fun onViewDestroyed() {
compositeDisposable.dispose()
}
}
This is my PresenterLoginTest class
@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
class PresenterLoginTest {
private lateinit var activity: ActivityLogin
@Mock
lateinit var view: PresenterLogin.View
@Mock
lateinit var service: ApiService
@Mock
lateinit var callLogin: Observable<Login>
private lateinit var presenter: PresenterLogin
@Before
fun setUp() {
activity = Robolectric.buildActivity(ActivityLogin::class.java)
.create()
.resume()
.get()
MockitoAnnotations.initMocks(this)
presenter = PresenterLogin(view)
}
@Test
@Throws(Exception::class)
fun shouldNotBeNull() {
assertNotNull(activity) // Success
}
@Test
fun requestLogin() {
Mockito.`when`(service.requestSignIn(
ApiHeaders().headers, "081212345678", "12345", ""))
.thenReturn(Observable.just(Login()))
presenter.requestLogin("081212345678", "12345")
verify(view).showProgressDialog() // Success
verify(view).onSuccessLogin()
/*
Wanted but not invoked:
view.onSuccessLogin();
-> at package.PresenterLoginTest.requestLogin(PresenterLoginTest.kt:117)
However, there was exactly 1 interaction with this mock:
view.showProgressDialog();
*/
}
}
showProgressDialog
method is called before mocked method so it passed verify (this is 1 interaction with this mock),If you mock method
requestSignIn
then it won't send a normal request which produce a response thatsubscribe
will atached to, and therefore also its nested methodonSuccessLogin
won't be executed.