简体   繁体   English

如何使用Powermock模拟void静态方法抛出异常?

[英]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.

相关问题 如何在powermock中模拟void方法 - how to mock a void method in powermock 如何模拟一种方法以返回某些东西而不是引发异常(PowerMock?) - How to mock a method to return something instead of throw exception (PowerMock?) PowerMock 不会模拟静态方法在 Spring-Boot 应用程序中抛出异常 - PowerMock won't mock static method to throw an Exception in a Spring-Boot application PowerMock,模拟使用静态方法的类的方法抛出nullpointerexception - PowerMock, mock a method of a class that uses a static method throw nullpointerexception 如何模拟void方法抛出异常? - How can I mock a void method to throw an exception? 如何在调用void方法时使用PowerMock / PowerMockito / Mockito抛出异常? - How to throw exception using PowerMock/PowerMockito/Mockito when a call to void method is made? 如何使用Powermock模拟静态方法? - How to mock static method using powermock? 如何在没有powermock的情况下模拟静态方法 - How to mock static method without powermock 在jmockit中,如何模拟void方法在第一个调用而不在后续调用上引发Exception? - In jmockit, how can I mock a void method to throw an Exception on the first call and not to on subsequent calls? Powermock:如何在静态类中模拟私有方法 - Powermock: How to mock a private method inside a static class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM