Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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()); 
like image 365
Smartmarkey Avatar asked Sep 20 '11 10:09

Smartmarkey


People also ask

How do you mock an exception to void method?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.

Can we mock static methods in Mockito?

Since static method belongs to the class, there is no way in Mockito to mock static methods.

What is difference between Mockito and PowerMock?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.


2 Answers

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.

like image 80
Smartmarkey Avatar answered Sep 21 '22 02:09

Smartmarkey


Or

PowerMockito.mockStatic(Adder.class); PowerMockito.doThrow(new IOException()).when(Adder.class, "add", Mathers.eq(12)); 
like image 38
kk1957 Avatar answered Sep 20 '22 02:09

kk1957