WIP: Auseinanderfusseln WIP:Service / Schlechter Unit-Test
authorKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 13:52:09 +0000 (14:52 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 13:52:09 +0000 (14:52 +0100)
src/main/java/de/juplo/demo/RemoteContentController.java [deleted file]
src/test/java/de/juplo/demo/RemoteContentServiceTest.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/demo/RemoteContentController.java b/src/main/java/de/juplo/demo/RemoteContentController.java
deleted file mode 100644 (file)
index 00b0b96..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-package de.juplo.demo;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-import reactor.core.publisher.Mono;
-
-
-/**
- * Fetches data from remote-webserver and renders them as HTML.
- * @author Kai Moritz
- */
-@RestController
-public class RemoteContentController
-{
-  @Autowired
-  RemoteContentService service;
-
-
-  @GetMapping("/")
-  public Mono<String> renderRemoteText(@RequestParam String path)
-  {
-    return service.getRemoteText(path);
-  }
-}
diff --git a/src/test/java/de/juplo/demo/RemoteContentServiceTest.java b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java
new file mode 100644 (file)
index 0000000..40a85b5
--- /dev/null
@@ -0,0 +1,50 @@
+package de.juplo.demo;
+
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
+import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
+import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Unit-Test for class {@link RemoteContentService}.
+ * @author Kai Moritz
+ */
+@ExtendWith(MockitoExtension.class)
+public class RemoteContentServiceTest
+{
+  RemoteContentService service;
+
+  @MockBean
+  WebClient webClient;
+  @MockBean
+  RequestHeadersUriSpec uriSpec;
+  @MockBean
+  RequestHeadersSpec headersSpec;
+  @MockBean
+  ResponseSpec responseSpec;
+
+
+  @Test
+  void test()
+  {
+    when(webClient.get()).thenReturn(uriSpec);
+    when(uriSpec.uri(eq("/foo"))).thenReturn(headersSpec);
+    when(headersSpec.retrieve()).thenReturn(responseSpec);
+    when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("bar"));
+
+    Mono<String> result = service.getRemoteText("/foo");
+
+    assertThat(result).isNotNull();
+    assertThat(result.block()).isEqualTo("bar");
+  }
+}