我试图模拟私人静态方法anotherMethod()
。见下面如何使用PowerMockito模拟私有静态方法?
public class Util {
public static String method(){
return anotherMethod();
}
private static String anotherMethod() {
throw new RuntimeException(); // logic was replaced with exception.
}
}
下面的代码是我测试代码
@PrepareForTest(Util.class)
public class UtilTest extends PowerMockTestCase {
@Test
public void should_prevent_invoking_of_private_method_but_return_result_of_it() throws Exception {
PowerMockito.mockStatic(Util.class);
PowerMockito.when(Util.class, "anotherMethod").thenReturn("abc");
String retrieved = Util.method();
assertNotNull(retrieved);
assertEquals(retrieved, "abc");
}
}
但每瓦我运行它,我得到这个例外
java.lang.AssertionError: expected object to not be null
我想我做错了与嘲讽东东。任何想法如何解决它?
号为'TestNG'我需要用我的注解。 – Aaron 2014-08-31 16:51:05