此代碼嘲笑類中的靜態void方法並覆蓋其行爲。Powermockito:攔截所有靜態方法
@RunWith(PowerMockRunner.class)
@PrepareForTest({Resource.class})
public class MockingTest{
@Test
public void shouldMockVoidStaticMethod() throws Exception {
PowerMockito.spy(Resource.class);
PowerMockito.doNothing().when(Resource.class, "readResources", Mockito.any(String.class));
//no exception heeeeere!
Resource.readResources("whatever");
PowerMockito.verifyStatic();
Resource.readResources("whatever");
}
}
class Resource {
public static void readResources(String someArgument) {
throw new UnsupportedOperationException("meh!");
}
public static void read(String someArgument) {
throw new UnsupportedOperationException("meh!");
}
}
我如何可以攔截所有的方法調用,而不是單獨指定的方法(從這個問題here兩者)?
它試圖PowerMockito.doNothing().when(Resource.class)
和PowerMockito.doNothing().when(Resource.class, Matchers.anything())
但這些不起作用。
沒有。不起作用。 – 2014-08-29 08:23:36