Unit-test with mocking, that does not determines the implementation
authorKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 18:51:09 +0000 (19:51 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 18:40:46 +0000 (19:40 +0100)
src/test/java/de/juplo/demo/RemoteContentServiceTest.java

index 60d1c5d..ac1488e 100644 (file)
@@ -1,18 +1,28 @@
 package de.juplo.demo;
 
 
+import java.net.URI;
+import java.nio.charset.Charset;
+import java.time.ZonedDateTime;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
 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.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.web.reactive.function.client.ClientResponse;
 import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
 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;
 
 
 /**
@@ -27,9 +37,13 @@ public class RemoteContentServiceTest
   @MockBean
   WebClient webClient;
   @MockBean
-  RequestHeadersUriSpec uriSpec;
+  RequestHeadersUriSpec headersSpec;
+  @MockBean
+  RequestBodyUriSpec bodySpec;
   @MockBean
   ResponseSpec responseSpec;
+  @MockBean
+  ClientResponse clientResponse;
 
 
   @BeforeEach
@@ -42,16 +56,51 @@ public class RemoteContentServiceTest
   @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> mono = Mono.just("bar");
+
+    when(webClient.options()).thenReturn(headersSpec);
+    when(webClient.head()).thenReturn(headersSpec);
+    when(webClient.get()).thenReturn(headersSpec);
+    when(webClient.method(any(HttpMethod.class))).thenReturn(bodySpec);
+    when(headersSpec.uri(any(URI.class))).thenReturn(headersSpec);
+    when(headersSpec.uri(any(String.class), any(Function.class))).thenReturn(headersSpec);
+    when(headersSpec.uri(any(String.class), any(Map.class))).thenReturn(headersSpec);
+    when(headersSpec.uri(any(String.class), (Object[])any())).thenReturn(headersSpec);
+    when(headersSpec.uri(any(Function.class))).thenReturn(headersSpec);
+    when(headersSpec.accept((MediaType[])any())).thenReturn(headersSpec);
+    when(headersSpec.acceptCharset((Charset[])any())).thenReturn(headersSpec);
+    when(headersSpec.attribute(any(String.class), any(Object.class))).thenReturn(headersSpec);
+    when(headersSpec.cookie(any(String.class), any(String.class))).thenReturn(headersSpec);
+    when(headersSpec.cookies(any(Consumer.class))).thenReturn(headersSpec);
+    when(headersSpec.exchange()).thenReturn(mono);
+    when(headersSpec.header(any(String.class), (String[])any())).thenReturn(headersSpec);
+    when(headersSpec.headers(any(Consumer.class))).thenReturn(headersSpec);
+    when(headersSpec.ifModifiedSince(any(ZonedDateTime.class))).thenReturn(headersSpec);
+    when(headersSpec.ifNoneMatch((String[])any())).thenReturn(headersSpec);
+    when(headersSpec.retrieve()).thenReturn(responseSpec);
+    when(bodySpec.uri(any(URI.class))).thenReturn(bodySpec);
+    when(bodySpec.uri(any(String.class), any(Function.class))).thenReturn(bodySpec);
+    when(bodySpec.uri(any(String.class), any(Map.class))).thenReturn(bodySpec);
+    when(bodySpec.uri(any(String.class), (Object[])any())).thenReturn(bodySpec);
+    when(bodySpec.uri(any(Function.class))).thenReturn(bodySpec);
+    when(bodySpec.accept((MediaType[])any())).thenReturn(bodySpec);
+    when(bodySpec.acceptCharset((Charset[])any())).thenReturn(bodySpec);
+    when(bodySpec.attribute(any(String.class), any(Object.class))).thenReturn(bodySpec);
+    when(bodySpec.cookie(any(String.class), any(String.class))).thenReturn(bodySpec);
+    when(bodySpec.cookies(any(Consumer.class))).thenReturn(bodySpec);
+    when(bodySpec.exchange()).thenReturn(Mono.just(clientResponse));
+    when(bodySpec.header(any(String.class), (String[])any())).thenReturn(bodySpec);
+    when(bodySpec.headers(any(Consumer.class))).thenReturn(bodySpec);
+    when(bodySpec.ifModifiedSince(any(ZonedDateTime.class))).thenReturn(bodySpec);
+    when(bodySpec.ifNoneMatch((String[])any())).thenReturn(bodySpec);
+    when(bodySpec.contentLength(any(Long.class))).thenReturn(bodySpec);
+    when(bodySpec.contentType(any(MediaType.class))).thenReturn(bodySpec);
+    when(bodySpec.retrieve()).thenReturn(responseSpec);
+    when(responseSpec.bodyToMono(String.class)).thenReturn(mono);
+    when(clientResponse.bodyToMono(String.class)).thenReturn(mono);
 
     Mono<String> result = service.getRemoteText("/foo");
 
-    StepVerifier
-        .create(result)
-        .expectNext("bar")
-        .verifyComplete();
+    assertThat(result).isSameAs(mono);
   }
 }