gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:59:26 24 4
gpt4 key购买 nike

我正在尝试测试这个类:

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点的地方是postman登录fun的最后一行:
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))
}

一旦 postman 被 mock ,为什么它会调用 login()的实际实现?乐趣?

最佳答案

除了打开 Postman 类之外,您还需要打开 login 方法才能模拟它。

所以 Postman 类需要是:

open class Postman(val mMessenger: TylltEndPoint) {

open fun login(email: String, password: String) {

// ...

}

}

这是用户中常见的问题/投诉, there is also an official all-open plugin这对你来说无处不在(如果你不想严格遵守禁止继承的原则,除非你为它设计)。

关于android - 模拟对象仍在调用方法(Mockito + Kotlin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41435079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com