使用PowerMockito的模拟私人方法仅从第二种方法开始工作
我试图模拟函数“privateMethod”以返回“新值”而不是“原始值”。函数“callPrivateMethod”的目的只是调用和测试私有方法。
class MyClass {
private fun privateMethod(): String = "Original value"
fun callPrivateMethod() = privateMethod()
}
我正在嘲笑多个来源中描述的私有方法。由于某种原因,该方法第一次返回 null。它仅在第二次正确返回“新值”时才起作用。
@RunWith(PowerMockRunner::class)
@PowerMockRunnerDelegate(JUnit4::class)
@PrepareForTest(MyClass::class)
class MyClassTest {
@Test
fun myTest() {
val newValue = "New value"
val myClass = MyClass()
val mySpy = spy(myClass)
PowerMockito.doReturn(newValue).`when`(mySpy, "privateMethod")
val v1 = mySpy.callPrivateMethod() // v1 == null
val v2 = mySpy.callPrivateMethod() // v2 == "New value"
// assertEquals(newValue, v1) // commented because it would fail
assertEquals(newValue, v2) // this works
}
}
我包含了必要的注释(也尝试过不使用“PowerMockRunnerDelegate”)。我还尝试使用 method(...) 函数,而不是将方法作为字符串传递。我尝试更新 junit 和 powermockito 依赖项。
I'm trying to mock the function 'privateMethod' to return "New value" instead of "Original value". The purpose of the function 'callPrivateMethod' is just to call and test the private method.
class MyClass {
private fun privateMethod(): String = "Original value"
fun callPrivateMethod() = privateMethod()
}
I'm mocking the private method as described in multiple sources. For some reason, for the first time, the method returns null. It only works the second time, when it correctly returns "New value".
@RunWith(PowerMockRunner::class)
@PowerMockRunnerDelegate(JUnit4::class)
@PrepareForTest(MyClass::class)
class MyClassTest {
@Test
fun myTest() {
val newValue = "New value"
val myClass = MyClass()
val mySpy = spy(myClass)
PowerMockito.doReturn(newValue).`when`(mySpy, "privateMethod")
val v1 = mySpy.callPrivateMethod() // v1 == null
val v2 = mySpy.callPrivateMethod() // v2 == "New value"
// assertEquals(newValue, v1) // commented because it would fail
assertEquals(newValue, v2) // this works
}
}
I included the neccesary annotations (also tried without the 'PowerMockRunnerDelegate'). I also tried using the method(...) function instead of passing the method as a string. I tried updating the junit and powermockito dependencies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了这个问题。我从
Mockito
而不是SPY> SPY
函数 powermockito 。另外,这是我的依赖性。以错误的方式混合它们会导致问题,因为存在兼容性问题。
testImplementation "junit:junit:4.13.1"
testImplementation "androidx.test:core:1.3.0"
testImplementation "androidx.arch.core:core-testing :2.1.0"
testImplementation "org.mockito:mockito-inline:3.3.3"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"< /code>
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-core:2.0.2"
tistimplation“ org.powermock:powermock-api-mockito2:2.0.2”
I found the problem. I was using the
spy
function fromMockito
instead of thespy
function fromPowerMockito
.Also, here are my dependencies. Mixing them in a wrong way causes problems because there are compatibility issues.
testImplementation "junit:junit:4.13.1"
testImplementation "androidx.test:core:1.3.0"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.mockito:mockito-inline:3.3.3"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-core:2.0.2"
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"