36

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());
Eric
  • 6,563
  • 5
  • 42
  • 66
Smartmarkey
  • 1,979
  • 5
  • 22
  • 25

3 Answers3

35

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.

sjngm
  • 12,423
  • 14
  • 84
  • 114
Smartmarkey
  • 1,979
  • 5
  • 22
  • 25
  • 2
    This is not intuitive at all. Struggled to get this working. Thanks for the tip. – Dhiraj Aug 28 '15 at 12:58
  • 1
    Note that `Adder.add(anyInt());` is not what triggers the exception here, it applies it to calls to `Adder.add()`. – sjngm Apr 26 '16 at 14:00
9

Or

PowerMockito.mockStatic(Adder.class);
PowerMockito.doThrow(new IOException()).when(Adder.class, "add", Mathers.eq(12));
kk1957
  • 8,246
  • 10
  • 41
  • 63
1

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.

Eric
  • 6,563
  • 5
  • 42
  • 66
John B
  • 32,493
  • 6
  • 77
  • 98
  • I read the tutorial here since I am using Mockito. http://code.google.com/p/powermock/wiki/MockitoUsage13 It does not mention I have to use replay mode. – Smartmarkey Sep 20 '11 at 11:44
  • John. Thanks a lot. But that is not the cause. I've got the answer. I will update my question. – Smartmarkey Sep 22 '11 at 09:23