From: Kai Moritz Date: Tue, 14 Jan 2020 19:11:06 +0000 (+0100) Subject: Verified, that RestController throws NotFound, if remote-data does not exist X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=b7dc861910bd43020ff91c8f9f51fe4c32a66aec;p=demos%2Ftesting Verified, that RestController throws NotFound, if remote-data does not exist * This tests may seem to check the value reached into the class by the test itself, because RestController simply returns the fetched Mono at the moment * But it is valuable, because it verifies, that the behavior stays unchanged, when the implementation of RestControler is changed --- diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java index 8b248ce..dae8929 100644 --- a/src/test/java/de/juplo/demo/RestControllerTest.java +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import static org.mockito.Mockito.when; +import org.springframework.web.reactive.function.client.WebClientResponseException; +import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -42,4 +44,24 @@ public class RestControllerTest .expectComplete() .verify(); } + + @Test + @DisplayName("Data not found on remote-server") + void testResponseNotFoud() + { + when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404))); + + Mono result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectError(NotFound.class) + .verify(); + } + + + WebClientResponseException exception(int status) + { + return WebClientResponseException.create(status, "", null, null, null); + } }