PowerMock 不验证私人电话不止一次
PowerMock not verifying private calls more than once
Class to be tested
public class ClassUnderTest {
public void functionA() {
functionB();
functionC();
}
private void functionB() {
}
private void functionC() {
}
}
测试Class
@RunWith(PowerMockRunner.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
}
执行测试时 class 我收到以下错误,
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at org.powermock.api.mockito.PowerMockito.verifyPrivate(PowerMockito.java:312)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
如果评论了一个验证,那么测试用例就可以正常工作。
您必须在 PrepareForTest 中添加 ClassUnderTest。
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
我刚刚在私有方法中添加了 sysouts & 运行 测试 class。
另一个想法:不要那样做。 不 验证 私有方法。这些方法是 private 是有原因的。你真的应该尽量避免编写必须知道调用了某个私有方法的测试。
你看,private 的想法是:它可能会发生变化。一个好的单元测试需要 观察 你的 class 的行为——你用不同的参数调用 public 方法;然后你检查返回的结果。或者,您可能使用依赖注入为您的测试提供模拟对象 class - 当您的测试代码需要那些其他方法来完成它的工作时。
但是你真的 不应该 开始检查私有方法。关键是:那些私有方法应该做一些从外部可以观察到的事情。他们要么处理最终的 "return" 值;要么或者他们改变了一些可以用其他方式检查的内部状态。换句话说:如果那些 private 方法不会对您的 class 测试产生任何其他影响 - 它们的目的是什么?!
你可以试试加两行代码
@RunWith(PowerMockRunner.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
PowerMockito.doNothing().when(classUnderTest, "functionB");
PowerMockito.doNothing().when(classUnderTest, "functionC");
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
}
Class to be tested
public class ClassUnderTest {
public void functionA() {
functionB();
functionC();
}
private void functionB() {
}
private void functionC() {
}
}
测试Class
@RunWith(PowerMockRunner.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
}
执行测试时 class 我收到以下错误,
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at org.powermock.api.mockito.PowerMockito.verifyPrivate(PowerMockito.java:312)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
如果评论了一个验证,那么测试用例就可以正常工作。
您必须在 PrepareForTest 中添加 ClassUnderTest。
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
我刚刚在私有方法中添加了 sysouts & 运行 测试 class。
另一个想法:不要那样做。 不 验证 私有方法。这些方法是 private 是有原因的。你真的应该尽量避免编写必须知道调用了某个私有方法的测试。
你看,private 的想法是:它可能会发生变化。一个好的单元测试需要 观察 你的 class 的行为——你用不同的参数调用 public 方法;然后你检查返回的结果。或者,您可能使用依赖注入为您的测试提供模拟对象 class - 当您的测试代码需要那些其他方法来完成它的工作时。
但是你真的 不应该 开始检查私有方法。关键是:那些私有方法应该做一些从外部可以观察到的事情。他们要么处理最终的 "return" 值;要么或者他们改变了一些可以用其他方式检查的内部状态。换句话说:如果那些 private 方法不会对您的 class 测试产生任何其他影响 - 它们的目的是什么?!
你可以试试加两行代码
@RunWith(PowerMockRunner.class)
public class TestClass {
@Test
public void testFunctionA() throws Exception {
ClassUnderTest classUnderTest = PowerMockito.spy(new ClassUnderTest());
PowerMockito.doNothing().when(classUnderTest, "functionB");
PowerMockito.doNothing().when(classUnderTest, "functionC");
classUnderTest.functionA();
PowerMockito.verifyPrivate(classUnderTest).invoke("functionB");
PowerMockito.verifyPrivate(classUnderTest).invoke("functionC");
}
}