title: How to Mock Spring WebFlux WebClient :Error
body: <p>Just a beginner in JUnit testing and I'm writing a small JUnit test coverage using Mockito for WebClient.</p>
  <p>Getting Connection refused: no further information: localhost/127.0.0.1:8080 <code>when(responseSpec.bodyToMono(String.class)).thenReturn(monoMock);</code>
  But as I have mocked request.retrieve, It should return me mockData.</p>
  <p>Any suggestion on Mocking of request.retrieve.bodyToMono.block() would be helpful.</p>
  <h1>Method</h1>
  <pre><code>
      public String getProfile(String auth) {
                String userProfileUrl = temp;
              
                WebClient.RequestHeadersSpec&lt;?&gt; request = WebClient.create().get().uri(URI.create(userProfileUrl))
                          .accept(MediaType.APPLICATION_JSON).header(AuthConstants.AUTHORIZATION, authorization);
          
                return request.retrieve()
                      .bodyToMono(String.class).block();
          }
  
  </code></pre>
  <h1>Test Case</h1>
  <pre><code>
      @Test
      public void testgetUserProfile() {
                  
                  //Mocking Web Client
              final WebClient.Builder webClientBuilder = Mockito.mock(WebClient.Builder.class);
              final WebClient webClient = Mockito.mock(WebClient.class);
              final WebClient.RequestHeadersUriSpec headerUriSpecMock = Mockito.mock(WebClient.RequestHeadersUriSpec.class);
              final WebClient.RequestHeadersSpec headersSpecMock= Mockito.mock(WebClient.RequestHeadersSpec.class);
              final WebClient.RequestBodyUriSpec bodyUriSpecMock = Mockito.mock(WebClient.RequestBodyUriSpec.class);
              final WebClient.RequestBodySpec bodySpecMock = Mockito.mock(WebClient.RequestBodySpec.class);
              final WebClient.ResponseSpec responseSpec = Mockito.mock(WebClient.ResponseSpec.class);
              final Mono res = Mockito.mock(Mono.class);
               
           
               
              Mockito.when(webClientBuilder.baseUrl(anyString())).thenReturn(webClientBuilder);
              Mockito.when(webClientBuilder.build()).thenReturn(webClient);
              Mockito.when(webClient.get()).thenReturn(headerUriSpecMock);
              Mockito.when(headerUriSpecMock.uri(anyString())).thenReturn(headersSpecMock);
              Mockito.when(headersSpecMock.accept(MediaType.APPLICATION_JSON)).thenReturn(headersSpecMock);
              Mockito.when(headersSpecMock.header(anyString())).thenReturn(headersSpecMock);
              
              Mockito.when(headersSpecMock.retrieve()).thenReturn(responseSpec);
          
              Mockito.when(responseSpec.bodyToMono(String.class)).thenReturn(monoMock);
              Mockito.when(monoMock.block()).thenReturn(userDetails);
              
              String tempuserDetails = authorizationUtil.getUserDetails(auth);
              assertNotNull(tempuserDetails);
          }
  
  </code></pre>
  <h1>CALL TRACE : ERROR#</h1>
  <pre><code>reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:8080
      at reactor.core.Exceptions.propagate(Exceptions.java:326)
      at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:87)
      at reactor.core.publisher.Mono.block(Mono.java:1162)
      at com.schneider_electric.dces.tnc.util.AuthorizationUtil.getUserProfile(AuthorizationUtil.java:94)
      at com.schneider_electric.dces.tnc.util.AuthorizationUtil.getUserDetails(AuthorizationUtil.java:74)
      at com.schneider_electric.dces.tnc.util.AuthorizationUtilTest.testgetUserProfile(AuthorizationUtilTest.java:116)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
      at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
      Suppressed: java.lang.Exception: #block terminated with an error
          at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:89)
          ... 35 more
  Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:8080
      at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
      at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
      at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325)
      at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340)
      at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:633)
      at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
      at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
      at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
      at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.net.ConnectException: Connection refused: no further information
  
  
   
  
  </code></pre>