【发布时间】:2014-10-24 23:44:27
【问题描述】:
我正在尝试模拟私有静态方法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
我想我在嘲笑东西方面做错了什么。有什么想法可以解决吗?
【问题讨论】:
标签: java unit-testing mockito