Implemented a seemingly pure and clean unit-test
authorKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 15:30:26 +0000 (16:30 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 15 Jan 2020 09:15:45 +0000 (10:15 +0100)
pom.xml
src/test/java/de/juplo/demo/RemoteContentServiceTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index d44e1ef..8da5bd0 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                </exclusion>
                        </exclusions>
                </dependency>
+               <dependency>
+                       <groupId>io.projectreactor</groupId>
+                       <artifactId>reactor-test</artifactId>
+                       <scope>test</scope>
+               </dependency>
 
                <dependency>
                        <groupId>org.springframework.boot</groupId>
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..60d1c5d
--- /dev/null
@@ -0,0 +1,57 @@
+package de.juplo.demo;
+
+
+import org.junit.jupiter.api.BeforeEach;
+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.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
+import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+
+/**
+ * Unit-Test for class {@link RemoteContentService}.
+ * @author Kai Moritz
+ */
+@ExtendWith(SpringExtension.class)
+public class RemoteContentServiceTest
+{
+  RemoteContentService service;
+
+  @MockBean
+  WebClient webClient;
+  @MockBean
+  RequestHeadersUriSpec uriSpec;
+  @MockBean
+  ResponseSpec responseSpec;
+
+
+  @BeforeEach
+  void setUp()
+  {
+    service = new RemoteContentService(webClient);
+  }
+
+
+  @Test
+  void test()
+  {
+    when(webClient.get()).thenReturn(uriSpec);
+    when(uriSpec.uri(eq("/foo"))).thenReturn(uriSpec);
+    when(uriSpec.retrieve()).thenReturn(responseSpec);
+    when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("bar"));
+
+    Mono<String> result = service.getRemoteText("/foo");
+
+    StepVerifier
+        .create(result)
+        .expectNext("bar")
+        .verifyComplete();
+  }
+}