我试图模拟私有静态方法anotherMethod()
。请参见下面的代码
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
我想我在模拟测试中做错了什么。有什么建议可以帮我解决吗?
@PrepareForTest({First.class,Util.class})
。 - Ilker Cat