Further simplification: using the real WebClient with an ExchangeFunction
[demos/testing] / src / test / java / de / juplo / demo / RemoteContentServiceTest.java
index a396d6c..cf2f035 100644 (file)
@@ -5,7 +5,12 @@ 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.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.ClientResponse;
+import org.springframework.web.reactive.function.client.ExchangeFunction;
+import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
 
 
@@ -18,11 +23,21 @@ public class RemoteContentServiceTest
 {
   RemoteContentService service;
 
+  @MockBean
+  ExchangeFunction exchangeFunction;
+  @MockBean
+  ClientResponse clientResponse;
+
 
   @BeforeEach
   void setUp()
   {
-    service = new RemoteContentService(WebClientStub.WEB_CLIENT);
+    service =
+        new RemoteContentService(
+            WebClient
+                .builder()
+                .exchangeFunction(request -> Mono.just(clientResponse))
+                .build());
   }
 
 
@@ -30,10 +45,10 @@ public class RemoteContentServiceTest
   void test()
   {
     Mono<String> mono = Mono.just("bar");
-    WebClientStub.expect(mono);
+    when(clientResponse.bodyToMono(String.class)).thenReturn(mono);
 
     Mono<String> result = service.getRemoteText("/foo");
 
-    assertThat(result).isSameAs(mono);
+    assertThat(result.block()).isEqualTo("bar");
   }
 }