Я пытаюсь запустить модульные тесты, используя MockitoJUnitRunner; они прекрасно работают в Android Studio, но некоторые (не все) тесты не выполняются при запуске в командной строке - что немаловажно, я должен иметь возможность запускать их с моей платформы непрерывной интеграции, а не только из IDE. Вот один из реальных тестируемых методов:
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()
}
})
}
Вот один из тестов, которые не пройдены:
@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)
}
}
А вот что я получаю, когда запускаю тесты в командной строке, используя ./gradlew testDebugUnitTest
> Task :app:testDebugUnitTest
com.domain.app.screens.main.home.HomePresenterTest > requestSecurityQuestion_onSuccess FAILED
org.mockito.exceptions.verification.WantedButNotInvoked at HomePresenterTest.kt:306
Также: я использую Android Studio 3.1.4, версия Gradle 3.1.2, версия Mockito 2.8.9