Verified correct behavior of RestController for 404
[demos/testing] / src / test / java / de / juplo / demo / RestControllerIT.java
index 0334b04..9d7a3f5 100644 (file)
@@ -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);
+  }
 }