使用返回整数列表的电源模拟测试私有方法
我有一个私有方法,它接受整数值列表,返回整数值列表。我如何使用电源模拟来测试它。我是 powermock 的新手。我可以用简单的模拟进行测试吗?如何..
I have a private method which take a list of integer value returns me a list of integer value. How can i use power mock to test it. I am new to powermock.Can i do the test with easy mock..? how..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自文档,在名为“常见 - 绕过封装”的部分中:
您还可以在同一部分中找到示例。
From the documentation, in the section called "Common - Bypass encapsulation":
You can also find examples in the same section.
这是如何执行此操作的完整示例:
Here is a full example how to do to it:
当您想使用 Powermockito 测试私有方法并且该私有方法具有语法:
在您的测试类方法中:
CustomClass[] params= new CustomClass[] {...}
WhiteboxImpl.invokeMethod(spy,"testmeMethod",params)
由于参数而无法工作。您收到一条错误消息,表明带有该参数的 testmeMethod 不存在
看这里:
WhiteboxImpl 类
对于 Array 类型的参数,PowerMock 很混乱。因此,在您的测试方法中将其修改为:
对于无参数私有方法,您不会遇到此问题。我记得它适用于 Primitve 类型和包装类的参数。
“理解 TDD 就是理解软件工程”
When you want to test a private method with Powermockito and this private method has syntax:
in your testing class method:
CustomClass[] params= new CustomClass[] {...}
WhiteboxImpl.invokeMethod(spy,"testmeMethod",params)
will not work because of params. you get an error message that testmeMethod with that arguments doesn't exist
Look here:
WhiteboxImpl class
For arguments of type Array, PowerMock is messed up. So modify this in your test method to:
You don't have this problem for parameterless private methods. As I can remember it works for parameters of type Primitve type and wrapper class.
"Understanding TDD is understanding Software Engineering"