From: Kai Moritz Date: Thu, 16 Jan 2020 08:58:52 +0000 (+0100) Subject: Verified correct behavior of RestController for 404 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ddda08de0026c84ed095dc76955f874fa7ef2dc6;p=demos%2Ftesting Verified correct behavior of RestController for 404 * Added @Test RestControllerIT.testResponseNotFound() * Fixed the behavior of the RestController accordingly * Adapted RestControllerTest accordingly --- diff --git a/src/main/java/de/juplo/demo/RestController.java b/src/main/java/de/juplo/demo/RestController.java index f6cddca..5d1d8f6 100644 --- a/src/main/java/de/juplo/demo/RestController.java +++ b/src/main/java/de/juplo/demo/RestController.java @@ -1,11 +1,13 @@ package de.juplo.demo; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.reactive.function.client.WebClientResponseException; import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound; +import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Mono; @@ -34,25 +36,21 @@ public class RestController .onErrorResume(t -> { if(t.getClass().equals(NotFound.class)) - return Mono.error(t); + return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Cause: " + t.getMessage(), t)); if(!(t instanceof WebClientResponseException)) return Mono.error( - WebClientResponseException.create( - 500, - "Internal Server Error - Cause: " + t.getMessage(), - null, - null, - null)); + new ResponseStatusException( + HttpStatus.INTERNAL_SERVER_ERROR, + "Cause: " + t.getMessage(), + t)); return Mono.error( - WebClientResponseException.create( - 503, - "Service Unavailable - Cause: " + t.getMessage(), - null, - null, - null)); + new ResponseStatusException( + HttpStatus.SERVICE_UNAVAILABLE, + "Cause: " + t.getMessage(), + t)); }); } } diff --git a/src/test/java/de/juplo/demo/RestControllerIT.java b/src/test/java/de/juplo/demo/RestControllerIT.java index 0334b04..9d7a3f5 100644 --- a/src/test/java/de/juplo/demo/RestControllerIT.java +++ b/src/test/java/de/juplo/demo/RestControllerIT.java @@ -2,6 +2,7 @@ package de.juplo.demo; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -13,6 +14,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; @@ -63,4 +65,28 @@ public class RestControllerIT .jsonPath("message").isEqualTo("Required String parameter 'path' is not present") .jsonPath("timestamp").exists(); } + + @DisplayName("Remote-Server answers with 404: Not Found") + @Test() + void testResponseNotFound() + { + when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404))); + webClient + .get() + .uri("/?path=foo") + .header("Accept", MediaType.TEXT_PLAIN_VALUE) + .exchange() + .expectStatus().isNotFound() + .expectBody() + .jsonPath("status").isEqualTo(404) + .jsonPath("error").isEqualTo("Not Found") + .jsonPath("path").isEqualTo("/") + .jsonPath("timestamp").exists(); + } + + + WebClientResponseException exception(int status) + { + return WebClientResponseException.create(status, "MESSAGE", null, null, null); + } } diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java index edf2280..4fcb8c9 100644 --- a/src/test/java/de/juplo/demo/RestControllerTest.java +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -11,9 +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.InternalServerError; -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; @@ -61,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(); } @@ -100,11 +104,14 @@ 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(); } @@ -122,8 +129,10 @@ public class RestControllerTest .create(result) .expectErrorSatisfies((t) -> { - assertThat(t).isInstanceOf(InternalServerError.class); - assertThat(t.getMessage()).isEqualTo("500 Internal Server Error - Cause: Boom!"); + 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(); } @@ -131,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); } }