gpt4 book ai didi

java - PowerMockito 验证调用 x 次的私有(private)方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:15:59 29 4
gpt4 key购买 nike

我正在使用 PowerMockitospy 来模拟私有(private)方法:

final SomeClass someClass = new SomeClass();
final SomeClass spy = PowerMockito.spy(someClass);

PowerMickito.doReturn("someValue", spy, "privateMethod1");
final String response = Whitebox.invokeMethod(spy, "anotherPrivateMethod");

// I can now verify `response` is of the correct data
// But I also want to verify `privateMethod1` was called x times or so

我不知道如何验证我的方法被调用了 x 次。

旁注

让我所有的私有(private)方法都受到protected,然后在我的测试类中扩展那个类并这样做是否更好?

最佳答案

这样就可以了。

PowerMockito.doReturn("someValue", spy, "privateMethod1");
final String response = Whitebox.invokeMethod(spy, "anotherPrivateMethod");
assert(response)
verifyPrivate(spy, times(1)).invoke("anotherPrivateMethod", "xyz");

我假设您的私有(private)方法 (anotherPrivateMethod) 采用一个参数“xyz”。您可以根据您的私有(private)方法声明对其进行修改。

关于java - PowerMockito 验证调用 x 次的私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48513063/

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