Verified, that RestController throws NotFound, if remote-data does not exist
authorKai Moritz <kai@juplo.de>
Tue, 14 Jan 2020 19:11:06 +0000 (20:11 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:18:03 +0000 (11:18 +0100)
* 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

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

index 8b248ce..dae8929 100644 (file)
@@ -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<String> result = controller.fetch("foo");
+
+    StepVerifier
+        .create(result)
+        .expectError(NotFound.class)
+        .verify();
+  }
+
+
+  WebClientResponseException exception(int status)
+  {
+    return WebClientResponseException.create(status, "", null, null, null);
+  }
 }