[英]How to mock a void static method to throw exception with Powermock?
I am trying to use Powermock and Mockito to mock a void static method to throw exception as below. 我试图使用Powermock和Mockito来模拟一个void静态方法来抛出异常,如下所示。 But I met a problem. 但我遇到了一个问题。 Unless I make the two invocations of Adder.add() with the same argument, the mocked IOException
won't be thrown. 除非我使用相同的参数对Adder.add()进行两次调用,否则不会抛出模拟的IOException
。
BTW, I've added @RunWith(PowerMockRunner.class)
and @PrepareForTest(Adder.class)
to the unit test class. 顺便说@RunWith(PowerMockRunner.class)
,我已经将@RunWith(PowerMockRunner.class)
和@PrepareForTest(Adder.class)
到单元测试类中。
class Adder{
public static void add(int i) throws IOException{
return;
}
}
@Test
public void testAdder() throws IOException{
PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class);
Adder.add(12);
try {
Adder.add(11);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// assert things
}
Thanks in advance. 提前致谢。 :) :)
Answer is as below. 答案如下。
After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. 在咨询http://code.google.com/p/powermock/issues/detail?id=278之后 ,实际上上面的Adder.add(12)是设置模拟静态方法的一部分。 It means when invoking Adder.add() with argument 12, IOException will be thrown. 这意味着在使用参数12调用Adder.add()时,将抛出IOException。 It is hard to understand, right? 这很难理解,对吧? :) So it should be written as below. :)所以它应该写如下。
PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class);
Adder.add(anyInt());
Answer is as below. 答案如下。
After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. 在咨询http://code.google.com/p/powermock/issues/detail?id=278之后 ,实际上上面的Adder.add(12)是设置模拟静态方法的一部分。 It means when invoking Adder.add() with argument 12, IOException will be thrown. 这意味着在使用参数12调用Adder.add()时,将抛出IOException。 It is hard to understand, right? 这很难理解,对吧? :) So it should be written as below. :)所以它应该写如下。
PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class);
Adder.add(anyInt());
EDIT: 编辑:
Link is dead, try Internet Archive one instead. 链接已死,请尝试使用Internet Archive 1。
Or 要么
PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class, "add", Mathers.eq(12));
Did you forget to put PowerMock in replay mode? 您是否忘记将PowerMock置于重放模式?
How to Mock Static methods. 如何模拟静态方法。
Per your link... 根据你的链接......
How to verify behavior Verification of a static method is done in two steps. 如何验证行为静态方法的验证分两步完成。 First call PowerMockito.verifyStatic()
to start verifying behavior and the call the static method you want to verify. 首先调用PowerMockito.verifyStatic()
以开始验证行为并调用要验证的静态方法。 Eg 例如
PowerMockito.verifyStatic();
Static.firstStaticMethod(param);
Important: You need to call verifyStatic()
per method verification. 重要提示:您需要按方法验证调用verifyStatic()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.