diff --git a/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java b/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java index 9b3b33c..d145b04 100644 --- a/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java +++ b/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java @@ -37,21 +37,28 @@ public void aNewUserShouldBeCreated() { } private TestResponse request(String method, String path) { + HttpURLConnection connection; try { URL url = new URL("http://localhost:4567" + path); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true); - connection.connect(); - String body = IOUtils.toString(connection.getInputStream()); - return new TestResponse(connection.getResponseCode(), body); - } catch (IOException e) { + //connection.connect(); + if(connection.getResponseCode()>=200 && connection.getResponseCode()<300) { + String body = IOUtils.toString(connection.getInputStream()); + return new TestResponse(connection.getResponseCode(), body); + } else { + String body = IOUtils.toString(connection.getErrorStream()); + return new TestResponse(connection.getResponseCode(), body); + } + } catch (IOException e) { e.printStackTrace(); - fail("Sending request failed: " + e.getMessage()); + org.junit.Assert.fail("Sending request failed: " + e.getMessage()); return null; } } + private static class TestResponse { public final String body;