How to mock a void static method to throw exception with Powermock?

前端 未结 3 1751
我在风中等你
我在风中等你 2020-12-15 03:23

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

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 04:25

    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.

提交回复
热议问题