Switched parameterized test in HtmlControllerTest to @EnumSource
authorKai Moritz <kai@juplo.de>
Tue, 14 Jan 2020 16:48:04 +0000 (17:48 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:03:10 +0000 (11:03 +0100)
src/test/java/de/juplo/demo/HtmlControllerTest.java

index 1584ef6..0aa7f13 100644 (file)
@@ -7,12 +7,13 @@ 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.junit.jupiter.params.provider.EnumSource;
 import org.mockito.ArgumentCaptor;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.HttpStatus;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.ui.Model;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
@@ -86,10 +87,26 @@ public class HtmlControllerTest
    */
   @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)
+  @EnumSource(value = HttpStatus.class, names = {
+    "BAD_REQUEST",
+    "UNAUTHORIZED",
+    "FORBIDDEN",
+    "METHOD_NOT_ALLOWED",
+    "NOT_ACCEPTABLE",
+    "CONFLICT",
+    "GONE",
+    "UNSUPPORTED_MEDIA_TYPE",
+    "TOO_MANY_REQUESTS",
+    "UNPROCESSABLE_ENTITY",
+    "INTERNAL_SERVER_ERROR",
+    "NOT_IMPLEMENTED",
+    "BAD_GATEWAY",
+    "SERVICE_UNAVAILABLE",
+    "GATEWAY_TIMEOUT"
+  })
+  void testOtherError(HttpStatus status)
   {
-    Mono<String> mono = Mono.error(WebClientResponseException.create(status, "", null, null, null));
+    Mono<String> mono = Mono.error(WebClientResponseException.create(status.value(), "", null, null, null));
     when(service.getRemoteText("foo")).thenReturn(mono);
 
     String result = controller.fetch(model, "foo");
@@ -99,7 +116,7 @@ public class HtmlControllerTest
     verify(model).addAttribute(eq("text"), captor.capture());
     StepVerifier
         .create(captor.getValue())
-        .expectNextMatches(message -> message.startsWith(Integer.toString(status)))
+        .expectNextMatches(message -> message.startsWith(Integer.toString(status.value())))
         .expectComplete()
         .verify();
   }