title: How to mock error response using MockWebServer in WebClient unit tests
body: <p>I am writing unit test for a method which uses WebClient to call a REST end point.
  Using MockWebServer, I am able to cover the code for a success response, but
  I am unable to find any way to simulate error response so that the related code pertaining to error handling also gets covered in unit tests.</p>
  <p>source class:</p>
  <pre><code>@Service
  @Slf4j
  public class EmployeeService {
  
     private final WebClient webClient;
     private final PaymentServiceConfiguration paymentServiceConfiguration;
     
     public EmployeeService(WebClient webClient, PaymentServiceConfiguration paymentServiceConfiguration){
        this.webClient = webClient;
        this.paymentServiceConfiguration= paymentServiceConfiguration;
  
     }
  
     public Mono&lt;PaymentDataResponse&gt; getPaymentInfo(PaymentDataRequest paymentDataRequest){
        return webClient
               .post()
               .uri(paymentServiceConfiguration.getBaseUri() + &quot;/payment&quot;)
               .body(Mono.just(paymentDataRequest, PaymentDataRequest.class)
               .retrieve()
               .onStatus(HttpStatus::isError, this.handleErrorResponse)
               .bodyToMono(PaymentDataResponse.class)
               .doOnError((throwable)-&gt; {
                   log.error(&quot;Exception in getPaymentInfo method. Message {}&quot;, throwable.getMessage());
                   throw(Exceptions.propagate(throwable));
               });
  
     }
  
     private Mono&lt;? extends Throwable&gt; handleErrorResponse(ClientResponse clientResponse){
        Mono&lt;String&gt; errorMessage = clientResponse.bodyToMono(String.class);
        return errorMessage.flatMap((message) -&gt; {
           log.error(&quot;Error response code is: {}, and the message is: {} &quot;, clientResponse.rawStatusCode(), message);
           return Mono.error(new RuntimeException(message));
        });
     }
  
  }
  </code></pre>
  <p>Test Class:</p>
  <pre><code>@RunWith(MockitoJunitRunner.class)
  public class EmployeeServiceTest {
     private MockWebServer server;
     private EmployeeService employeeService;
     private PaymentServiceConfiguration paymentServiceConfiguration;
     private String paymentServiceUri;
  
     @Before
     public void setUp() throws IOException{
       paymentServiceConfiguration = mock(PaymentServiceConfiguration.class);
       paymentServiceUri = &quot;/paymentdomain.com&quot;;
       employeeService = new EmployeeService(WebClient.create(), paymentServiceConfiguration);
       server = new MockWebServer();
       
       server.start();
     }
  
    @Test
    public testPaymentInfo() throws IOException, InterruptedException {
       when(paymentServiceConfiguration.getBaseUri()).thenReturn(server.url(paymentServiceUri).url.toString());
  
       String result = &quot;{\&quot;paymentId\&quot;: 101, &quot;\paymentType\&quot;: \&quot;online\&quot;}&quot;;
  
       server.enque(
           new MockResponse.setResponseCode(200)
                   .setHeader(&quot;content-type&quot;, &quot;application/json&quot;)
                   .setBody(result);
       );
       
       StepVerifier.create(employeeService.getPaymentInfo(new PaymentDataRequest())
            .expectNext(new ObjectMapper.readValue(result, PaymentDataResponse.class))
            .expectComplete()
            .verify();
       
       RecordRequest request = server.takeRequest();
       Assert.assertEquals(&quot;POST&quot;, request.getMethod());
       Assert.assertEquals(&quot;/paymentdomain.com/payment&quot;, request.getPath());
    }
  
    @After
    public void tearDown() throws IOException {
       server.shutdown();
    }
     
  }
  </code></pre>
  <p>Above test covers the happy path of code and marks the code coverage appropriately. How can I mock an error here, so that it can cover the below lines in source class code (regarding error scenario)</p>
  <p>// .onStatus(HttpStatus::isError, this.handleErrorResponse)</p>
  <p>// .doOnError((throwable)-&gt; { ......</p>