使用PowerMockito的模拟私人方法仅从第二种方法开始工作

发布于 2025-01-19 16:42:52 字数 1022 浏览 5 评论 0原文

我试图模拟函数“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2025-01-26 16:42:52

我发现了这个问题。我从Mockito而不是SPY> SPY函数 powermockito 。

val mySpy = PowerMockito.spy(myClass)

另外,这是我的依赖性。以错误的方式混合它们会导致问题,因为存在兼容性问题。

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 from Mockito instead of the spy function from PowerMockito.

val mySpy = PowerMockito.spy(myClass)

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文