[英]Verify private static method on final class gets called using PowerMockito
[英]PowerMockito verify private method called x times
我正在使用PowerMockito
和spy
来模拟私有方法:
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次。
边注
最好只protected
我所有的私有方法,然后在测试类中扩展该类并做到这一点呢?
这样就可以了。
PowerMockito.doReturn("someValue", spy, "privateMethod1");
final String response = Whitebox.invokeMethod(spy, "anotherPrivateMethod");
assert(response)
verifyPrivate(spy, times(1)).invoke("anotherPrivateMethod", "xyz");
我假设您的私有方法(anotherPrivateMethod)接受一个参数“ xyz”。 您可以根据私有方法声明进行修改。
测试私有方法就是测试实现细节。 如果选择更改实现,则测试将变得一文不值,并且将被丢弃。
您应该努力测试什么,而不是方法。 这样,即使您更改实施,测试也将继续存在。
可以在这里找到关于如何避免测试偶然细节的精彩论文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.