From: Kai Moritz Date: Tue, 14 Jan 2020 16:26:16 +0000 (+0100) Subject: Checked, that the error-messeage is rendered for other common errors X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=24963bcd949bde78e6b9655b29bceffb525521d0;p=demos%2Ftesting Checked, that the error-messeage is rendered for other common errors * Added a unit-test, that defines the expected behavior for other errors * Since the HtmlController simply used getMessage(), no modification was necessary to comply with the added test --- diff --git a/src/test/java/de/juplo/demo/HtmlControllerTest.java b/src/test/java/de/juplo/demo/HtmlControllerTest.java index aede077..1584ef6 100644 --- a/src/test/java/de/juplo/demo/HtmlControllerTest.java +++ b/src/test/java/de/juplo/demo/HtmlControllerTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.BeforeEach; 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; import org.mockito.ArgumentCaptor; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; @@ -76,4 +78,29 @@ public class HtmlControllerTest .expectComplete() .verify(); } + + /** + * Only the behavior on the common errors, as defined in {@link + * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest) + * WebClientResponseException.create()} is tested. + */ + @DisplayName("Other error while fetching data from remote-server") + @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}") + @ValueSource(ints = { 400, 401, 403, 405, 406, 409, 410, 415, 422, 429, 500, 501, 502, 503, 504 }) + void testOtherError(int status) + { + Mono mono = Mono.error(WebClientResponseException.create(status, "", null, null, null)); + when(service.getRemoteText("foo")).thenReturn(mono); + + String result = controller.fetch(model, "foo"); + + assertThat(result).isEqualTo("home"); + ArgumentCaptor> captor = ArgumentCaptor.forClass(Mono.class); + verify(model).addAttribute(eq("text"), captor.capture()); + StepVerifier + .create(captor.getValue()) + .expectNextMatches(message -> message.startsWith(Integer.toString(status))) + .expectComplete() + .verify(); + } }