Checked, that the error-messeage is rendered for other common errors
authorKai Moritz <kai@juplo.de>
Tue, 14 Jan 2020 16:26:16 +0000 (17:26 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:03:10 +0000 (11:03 +0100)
* 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

src/test/java/de/juplo/demo/HtmlControllerTest.java

index aede077..1584ef6 100644 (file)
@@ -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<String> 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<Mono<String>> 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();
+  }
 }