0

In my unit test, I currently have this code:

int responseCode;
try {
    WebResponse response = new WebConversation().getResponse(new GetMethodWebRequest("http://myurl"));
    responseCode = response.getResponseCode();
} catch (HttpException e) {
    responseCode = e.getResponseCode();
}
assertThat(responseCode).isEqualTo(403);

This is quite verbose. I would prefer to use something like this:

WebConversation wc = new WebConversation();
wc.doNotFail(); // <= does not exist
assertThat(wc.getResponse(new GetMethodWebRequest("http://myurl")).getResponseCode()).isEqualTo(403);

How can I tell HttpUnit to not fail for http status 4xx?

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • What the documentation says ? Can you provide the `wc` type, I don't know `HttpUnit – AxelH Mar 16 '18 at 12:47
  • 1
    You see, it was in the documentatino ;) I had a unplanned meeting ... couldn't check sooner. – AxelH Mar 16 '18 at 14:03

1 Answers1

0

You can use (for each test):

    wc.setExceptionsThrownOnErrorStatus(false);

or (for all tests):

    HttpUnitOptions.setExceptionsThrownOnScriptError(false);
slartidan
  • 20,403
  • 15
  • 83
  • 131
  • 1
    Here is the [`WebConversation.setExceptionsThrownOnErrorStatus`](http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/WebClient.html#setExceptionsThrownOnErrorStatus%28boolean%29) documentation. – AxelH Mar 16 '18 at 14:02