尊敬的stackoverflow同志们, 我再次遇到了让特定的PowerMock/Mockito案例工作的问题。 问题是,我需要验证从公共非静态方法调用的私有静态方法的调用。类似的例子我以前张贴在How to suppress and verify private static method calls?Powermock验证非静态方法中的私有静态方法调用
这是我的代码:
class Factory {
public String factorObject() throws Exception {
String s = "Hello Mary Lou";
checkString(s);
return s;
}
private static void checkString(String s) throws Exception {
throw new Exception();
}
}
,这是我的TestClass:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Factory.class)
public class Tests extends TestCase {
public void testFactory() throws Exception {
Factory factory = mock(Factory.class);
suppress(method(Factory.class, "checkString", String.class));
String s = factory.factorObject();
verifyPrivate(factory, times(8000)).invoke("checkString", anyString());
}
}
这里的问题是,该测试是成功的,但它不应该。它不应该是因为私有静态方法应该被调用1次。但是,无论我放在什么时候(),它总是验证它是真实的... halp :(
你真不该测试'private'方法此密切夫妇。你的单元测试的结果是_pain_,为什么不检查'class'是否应该抛出'Exception' – 2013-05-13 06:22:42
基本上你是对的,但不幸在我的* real *项目中,条件如何以及何时发生异常被抛出隐藏在密码中,这就是为什么我要走了为压制行为。我只需要验证对它的呼叫。 – Malvin 2013-05-13 06:42:38
这已经在http://stackoverflow.com/questions/14065515/mockito-bypass-static-method-for-testing回答了 – Rudy 2013-05-13 06:44:20