5

I'm trying to run unit tests using MockitoJUnitRunner; they run fine under Android Studio, but some (not all) tests fail when running in the command line - which is a big deal, I need to be able to run them from my continuous integration platform, not just from an IDE. Here's one of the the actual method being tested:

internal fun requestSecurityQuestion() {
    if (isViewAttached) mvpView.showLoadingDialog()

    api.getSecurityQuestionToday(mDataManager.token,
            ACTION_ASK_SECURITY_QUESTION_ON_LOGIN, 0)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object : CallbackWrapper<SettingsResponse>(mDataManager) {
                override fun onSuccess(response: SettingsResponse) {
                    mDataManager.securityQuestion = response.question
                    mDataManager.processId = response.processId

                    if (isViewAttached) {
                        mvpView.dismissLoadingDialog()
                        mvpView.showVsecVerificationDialog(0, response.question!!)
                    }
                }

                override fun onFailed(errorId: Int, jsonResponse: String?) {
                    if (isViewAttached) mvpView.dismissLoadingDialog()
                }
            })
}

Here's one of the tests that fail:

@RunWith(MockitoJUnitRunner::class)
class HomePresenterTest {

@Mock
private var mView: HomeView? = null
@Mock
private var mDataManager: DataManager? = null
@Mock
private var mApi: Api? = null

lateinit var mPresenter: HomePresenter

@Before
fun setUp() {
    mPresenter = spy(HomePresenter(mDataManager!!))
    mPresenter.attachView(mView!!)
}

@Test
fun requestSecurityQuestion_onSuccess() {
    val response = SettingsResponse()
    response.question = TestData.secretQuestion
    response.processId = TestData.processId

    `when`(mPresenter.api).thenReturn(mApi)
    `when`(mDataManager!!.token).thenReturn(TestData.userToken)
    `when`<Observable<SettingsResponse>>(mApi!!.getSecurityQuestionToday(
            TestData.userToken, ACTION_ASK_SECURITY_QUESTION_ON_LOGIN, 0))
            .thenReturn(Observable.just(response))

    mPresenter.requestSecurityQuestion()

    verify<HomeView>(mView).dismissLoadingDialog()
    verify<HomeView>(mView).showVsecVerificationDialog(0, TestData.secretQuestion)
}
}

And here's what I get when running the tests on command line using ./gradlew testDebugUnitTest

> Task :app:testDebugUnitTest 

com.domain.app.screens.main.home.HomePresenterTest > requestSecurityQuestion_onSuccess FAILED
org.mockito.exceptions.verification.WantedButNotInvoked at HomePresenterTest.kt:306

Also: I'm using Android Studio 3.1.4, Gradle version is 3.1.2, Mockito's version is 2.8.9

2
  • have you found your answer? Commented Dec 20, 2019 at 11:59
  • have you found your answer? Commented Jun 24, 2020 at 20:57

1 Answer 1

0

I had the same problem, but with Java.

The reason for me was, that I tried to return a mock on a method that was called in a static context, therefor wasn't bothered by my Mockito.when() call. Even though it is bad practice to do so, I moved the method call out of the static context, because there was no need for it to be static, and the it worked.

Maybe that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.