此代码嘲笑类中的静态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