X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FRestControllerTest.java;h=4fcb8c91db3f63174c162692ab25010acefe56ec;hb=refs%2Fheads%2Fmaster;hp=4b4526a7b20a41d0085217ac593bbc2bec7b493f;hpb=d254e8fa52ab3897c7d7a95e36d9b93635d7d7c6;p=demos%2Ftesting diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java index 4b4526a..4fcb8c9 100644 --- a/src/test/java/de/juplo/demo/RestControllerTest.java +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -11,8 +11,7 @@ import org.mockito.Mockito; import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClientResponseException; -import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound; -import org.springframework.web.reactive.function.client.WebClientResponseException.ServiceUnavailable; +import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -60,7 +59,13 @@ public class RestControllerTest StepVerifier .create(result) - .expectError(NotFound.class) + .expectErrorSatisfies((t) -> + { + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException status = (ResponseStatusException)t; + assertThat(status.getStatus()).isEqualTo(HttpStatus.NOT_FOUND); + assertThat(t.getMessage()).startsWith("404 NOT_FOUND \"Cause: 404 MESSAGE\";"); + }) .verify(); } @@ -99,11 +104,35 @@ public class RestControllerTest .create(result) .expectErrorSatisfies((t) -> { - assertThat(t).isInstanceOf(ServiceUnavailable.class); + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException e = (ResponseStatusException)t; + assertThat(e.getStatus()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); assertThat(t.getMessage()) .startsWith( - "503 Service Unavailable - Cause: " + - Integer.toString(status.value())); + "503 SERVICE_UNAVAILABLE \"Cause: " + + Integer.toString(status.value()) + + " MESSAGE\";"); + }) + .verify(); + } + + @Test + @DisplayName("Internal error while fetching data from remote-server") + void testOtherErrors() + { + Mono mono = Mono.error(new RuntimeException("Boom!")); + when(service.getRemoteText("foo")).thenReturn(mono); + + Mono result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectErrorSatisfies((t) -> + { + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException e = (ResponseStatusException)t; + assertThat(e.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(t.getMessage()).startsWith("500 INTERNAL_SERVER_ERROR \"Cause: Boom!\";"); }) .verify(); } @@ -111,6 +140,6 @@ public class RestControllerTest WebClientResponseException exception(int status) { - return WebClientResponseException.create(status, "", null, null, null); + return WebClientResponseException.create(status, "MESSAGE", null, null, null); } }