0

I am writing J-Unit Tests for my project and now this problem occured:

I am testing a servlet which uses a Utility class (class is final, all methods are static). The used method returns void and can throw an

IOException (httpResponse.getWriter).

Now I have to force this exception...

I have tried and searched a lot, but all the solutions I found did not worked, because there was no combination of final, static, void, throw.

Has anyone did this before?

EDIT: Here is the code snippet

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
    String action = request.getParameter("action");
    if (action.equals("saveRule")) {
        // Some code
        String resp = "blablabla";
        TOMAMappingUtils.evaluateTextToRespond(response, resp);
    }
} catch (IOException e) {
    TOMAMappingUtils.requestErrorHandling(response, "IOException", e);
}

}

Utils Class:

public final class TOMAMappingUtils {
private static final Logger LOGGER = Logger.getLogger(TOMAMappingUtils.class.getName());
private static final Gson GSON = new Gson();

public static void evaluateTextToRespond(HttpServletResponse response, String message) throws IOException {
    // Some Code
    response.getWriter().write(new Gson().toJson(message));

}

}

Test Method:

@Test
public void doPostIOException () {
    // Set request Parameters
    when(getMockHttpServletRequest().getParameter("action")).thenReturn("saveRule");
    // Some more code
    // Make TOMAMappingUtils.evaluateTextToRespond throw IOExpection to jump in Catch Block for line coverage
    when(TOMAMappingUtils.evaluateTextToRespond(getMockHttpServletResponse(), anyString())).thenThrow(new IOException()); // This behaviour is what i want
}

So as you can see, i want to force the Utils method to throw an IOException, so i get in the catch block for a better line coverage.

  • What do you mean by "Now I have to force this exception"? Why? In what context? What are you actually trying to achieve? What does the method do? – Jon Skeet Jul 10 '15 at 06:39
  • Added some code snippets for a better discription of my problem – Dominik Schmitz Jul 10 '15 at 07:32
  • So it sounds like you just need a writer that will throw an IOException, right? – Jon Skeet Jul 10 '15 at 07:47
  • Yeah, i thought about this already, but the problem is, that this function will be called a second time after i force this IOException and then i will get an IOException i don't want to have... – Dominik Schmitz Jul 10 '15 at 07:56
  • With the same writer? Why would it do that? That sounds very odd. – Jon Skeet Jul 10 '15 at 08:09
  • yeah, sounds strange to me too. the developer who is responsible for this tried to send information to the FrontEnd with the writer. When an IOException occures, he want to send an error message via the same writer who threw the IOException, this would not work anyway, am i right?? – Dominik Schmitz Jul 10 '15 at 09:20
  • So think about that. In particular, if you've failed to write a valid response, you're unlikely to be able to write an error response... – Jon Skeet Jul 10 '15 at 09:21
  • Are you using powermockito framework ? – kswaughs Jul 10 '15 at 09:23
  • thanks, thats what i thought :) then, could you please tell me: how do i force the writer to throw an IOEx? – Dominik Schmitz Jul 10 '15 at 09:23
  • regularly i am using mockito, but its possible to use PowerMockito :) – Dominik Schmitz Jul 10 '15 at 09:30

1 Answers1

0

To mock final class, First add it in prepareForTest.

@PrepareForTest({ TOMAMappingUtils.class })

Then mock as static class

PowerMockito.mockStatic(TOMAMappingUtils.class);

Then set expectation as below.

PowerMockito.doThrow(new IOException())
    .when(TOMAMappingUtils.class,
            MemberMatcher.method(TOMAMappingUtils.class,
                    "evaluateTextToRespond",HttpServletResponse.class, String.class ))
    .withArguments(Matchers.anyObject(), Matchers.anyString());

Another way:

PowerMockito
    .doThrow(new IOException())
    .when(MyHelper.class, "evaluateTextToRespond", 
             Matchers.any(HttpServletResponse.class), Matchers.anyString());
durron597
  • 31,968
  • 17
  • 99
  • 158
kswaughs
  • 2,967
  • 1
  • 17
  • 21