我有一个类,其中包含示例中的一些方法。
public class TestClass {
public boolean aMethod()
{
voidMethod();
return true;
}
private void voidMethod()
{
... does something ...
}
... other methods ...
}
我想用 powermock 测试 aMethod
,除 voidMethod
外,所有方法都应该正常工作。
我已经创建了 TestClass
的部分模拟以使 voidMethod
不执行任何操作。但我不知道如何调用此方法。
testObject = createPartialMock(TestClass.class, "voidMethod");
expectPrivate(testObject, "voidMethod");
我在第二行遇到错误:
The method expect(T) in the type EasyMock is not applicable for the arguments (void)
我该如何解决这个问题?
最佳答案
像这样使用简单的调用:
testObject.voidMethod(); // don't use "expect" for voids
expectLastCall().times(3); // use this for expectations
并且不要忘记 reply()
在所有你的期望之后和 verify()
在运行测试代码之后。
关于java - 如何使用 PowerMock 测试从同一类调用另一个私有(private) void 方法的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426988/