模拟对象仍在调用方法(Mockito + Kotlin)

最后发布: 2017-01-03 01:00:49


问题

我正在尝试测试此类:

class LoginPresenter(val mPostman: Postman) : ContractLoginPresenter, Validator.ValidationListener {

    private var view: ContractLoginView? = null

    override fun setView(_view: BaseView) {
        view = _view as ContractLoginView
    }

    override fun getValidationListener(): Validator.ValidationListener {
        return this
    }

    override fun onValidationSucceeded() {
        view!!.getContext().showCircularProgressBar()
        mPostman.login(view!!.getUsername(), view!!.getPassword())
    }

为此,我创建了这个测试类:

@RunWith(PowerMockRunner::class)
@PrepareForTest(App::class)
class TestLoginPresenter {

    var mockedPostman = mock(Postman::class.java)
    var mockedComponent = mock(MainComponent::class.java)
    var mockedView = mock(ContractLoginView::class.java)
    var mockedContext = mock(AppCompatActivity::class.java)

    var loginPresenter: LoginPresenter? = null

    @Before
    fun setUp() {
        PowerMockito.mockStatic(App::class.java)
        `when`(App.component).thenReturn(mockedComponent)
        loginPresenter = LoginPresenter(mockedPostman)
        loginPresenter!!.setView(mockedView)
        `when`(mockedView.getContext()).thenReturn(mockedContext)
    }

    @Test
    fun testGetValidationListener() {
        assertEquals(loginPresenter!!.getValidationListener(), loginPresenter)
    }

    @Test
    fun testOnValidationSucceeded() {
        val password = "password"
        val username = "username"
        `when`(mockedView.getPassword()).thenReturn(password)
        `when`(mockedView.getUsername()).thenReturn(username)
        //Mockito.doNothing().`when`(mockedPostman).login(anyString(), anyString())

        loginPresenter!!.onValidationSucceeded()
        verify(mockedPostman.login(username, password))
    }

以下堆栈跟踪失败:

java.lang.NullPointerException
    at br.com.tyllt.controller.Postman.login(Postman.kt:26)
    at br.com.tyllt.presenter.LoginPresenter.onValidationSucceeded(LoginPresenter.kt:43)
    at br.com.tyllt.presenter.TestLoginPresenter.testOnValidationSucceeded(TestLoginPresenter.kt:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


Process finished with exit code -1

stacktrace点是邮递员登录乐趣的最后一行的位置:

open class Postman(val mMessenger: TylltEndPoint) {

    fun login(email: String, password: String) {
        val user = User()
        user.email = email
        user.password = password

        mMessenger.login(user).enqueue(ResponseWrapperCallback(EventCatalog.e0014))
    }

一旦邮递员被嘲笑,为什么它实际上调用了login()的实际实现呢?

android unit-testing mockito kotlin powermockito
回答

因为您告诉它调用方法。 这个

verify(mockedPostman.login(username, password))

应该

 verify(mockedPostman).login(username, password)