如何使用 Powermock 模拟 void 静态方法以引发异常?
我正在尝试使用 Powermock 和 Mockito 来模拟 void 静态方法来抛出异常,如下所示。但我遇到了一个问题。除非我使用相同的参数两次调用 Adder.add(),否则不会抛出模拟的 IOException。
顺便说一句,我已将 @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
}
提前致谢。 :)
答案如下。
在此处咨询后 http://code.google.com/p/powermock/issues/detail?id=278 ,实际上上面的 Adder.add(12) 是设置模拟静态方法的一部分。这意味着当使用参数 12 调用 Adder.add() 时,将抛出 IOException。很难理解吧? :) 所以应该写成下面这样。
PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class);
Adder.add(anyInt());
I am trying to use Powermock and Mockito to mock a void static method to throw exception as below. 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.
BTW, I've added @RunWith(PowerMockRunner.class)
and @PrepareForTest(Adder.class)
to the unit test 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. It means when invoking Adder.add() with argument 12, IOException will be thrown. 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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案如下。
在此咨询后http: //code.google.com/p/powermock/issues/detail?id=278 ,实际上上面的 Adder.add(12) 是设置模拟静态方法的一部分。这意味着当使用参数 12 调用 Adder.add() 时,将抛出 IOException。很难理解吧? :) 所以应该写成下面这样。
编辑:
链接已失效,请尝试 互联网档案馆 改为之一。
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. It means when invoking Adder.add() with argument 12, IOException will be thrown. It is hard to understand, right? :) So it should be written as below.
EDIT:
Link is dead, try Internet Archive one instead.
或者
Or
您是否忘记将 PowerMock 置于重放模式?
如何模拟静态方法。
根据您的链接...
如何验证行为
静态方法的验证分两步完成。首先调用
PowerMockito.verifyStatic()
开始验证行为并调用您要验证的静态方法。例如,重要提示:您需要为每个方法验证调用
verifyStatic()
。Did you forget to put PowerMock in replay mode?
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. E.g.Important: You need to call
verifyStatic()
per method verification.