简体   繁体   English

MockitoJUnit 测试在 Android Studio 中运行但在命令行上运行时失败

[英]MockitoJUnit tests running in Android Studio but failed when running on the command line

I'm trying to run unit tests using MockitoJUnitRunner;我正在尝试使用 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.它们在 Android Studio 下运行良好,但在命令行中运行时某些(并非全部)测试失败 - 这很重要,我需要能够从我的持续集成平台运行它们,而不仅仅是从 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这是我在使用./gradlew testDebugUnitTest在命令行上运行测试时得到的./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另外:我使用的是 Android Studio 3.1.4,Gradle 版本是 3.1.2,Mockito 的版本是 2.8.9

I had the same problem, but with Java.我有同样的问题,但使用 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.我的原因是,我试图返回一个在静态上下文中调用的方法的模拟,因此没有被我的Mockito.when()调用所困扰。 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.也许这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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