如何使用 Powermock 模拟 void 静态方法以引发异常?

发布于 2024-12-05 15:04:57 字数 1146 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房的律动 2024-12-12 15:04:57

答案如下。

在此咨询后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());

编辑:
链接已失效,请尝试 互联网档案馆 改为之一。

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());

EDIT:
Link is dead, try Internet Archive one instead.

凉风有信 2024-12-12 15:04:57

或者

PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class, "add", Mathers.eq(12));

Or

PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class, "add", Mathers.eq(12));
樱娆 2024-12-12 15:04:57

您是否忘记将 PowerMock 置于重放模式?

如何模拟静态方法。

根据您的链接...

如何验证行为
静态方法的验证分两步完成。首先调用 PowerMockito.verifyStatic() 开始验证行为并调用您要验证的静态方法。例如,

 PowerMockito.verifyStatic();
 Static.firstStaticMethod(param);

重要提示:您需要为每个方法验证调用 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.

 PowerMockito.verifyStatic();
 Static.firstStaticMethod(param);

Important: You need to call verifyStatic() per method verification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文