Implemented a WebClientStub to simplify the unit-test
[demos/testing] / src / test / java / de / juplo / demo / RemoteContentServiceTest.java
index ac1488e..a396d6c 100644 (file)
@@ -1,27 +1,11 @@
 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.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;
 
 
@@ -34,22 +18,11 @@ public class RemoteContentServiceTest
 {
   RemoteContentService service;
 
-  @MockBean
-  WebClient webClient;
-  @MockBean
-  RequestHeadersUriSpec headersSpec;
-  @MockBean
-  RequestBodyUriSpec bodySpec;
-  @MockBean
-  ResponseSpec responseSpec;
-  @MockBean
-  ClientResponse clientResponse;
-
 
   @BeforeEach
   void setUp()
   {
-    service = new RemoteContentService(webClient);
+    service = new RemoteContentService(WebClientStub.WEB_CLIENT);
   }
 
 
@@ -57,47 +30,7 @@ public class RemoteContentServiceTest
   void test()
   {
     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);
+    WebClientStub.expect(mono);
 
     Mono<String> result = service.getRemoteText("/foo");