我試圖模擬私人靜態方法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