title: How to mock Spring WebFlux WebClient?
body: <p>We wrote a small Spring Boot REST application, which performs a REST request on another REST endpoint.</p>
  
  <pre><code>@RequestMapping("/api/v1")
  @SpringBootApplication
  @RestController
  @Slf4j
  public class Application
  {
      @Autowired
      private WebClient webClient;
  
      @RequestMapping(value = "/zyx", method = POST)
      @ResponseBody
      XyzApiResponse zyx(@RequestBody XyzApiRequest request, @RequestHeader HttpHeaders headers)
      {
          webClient.post()
              .uri("/api/v1/someapi")
              .accept(MediaType.APPLICATION_JSON)
              .contentType(MediaType.APPLICATION_JSON)
              .body(BodyInserters.fromObject(request.getData()))
              .exchange()
              .subscribeOn(Schedulers.elastic())
              .flatMap(response -&gt;
                      response.bodyToMono(XyzServiceResponse.class).map(r -&gt;
                      {
                          if (r != null)
                          {
                              r.setStatus(response.statusCode().value());
                          }
  
                          if (!response.statusCode().is2xxSuccessful())
                          {
                              throw new ProcessResponseException(
                                      "Bad status response code " + response.statusCode() + "!");
                          }
  
                          return r;
                      }))
              .subscribe(body -&gt;
              {
                  // Do various things
              }, throwable -&gt;
              {
                  // This section handles request errors
              });
  
          return XyzApiResponse.OK;
      }
  }
  </code></pre>
  
  <p>We are new to Spring and are having trouble writing a Unit Test for this small code snippet.</p>
  
  <p>Is there an elegant (reactive) way to mock the webClient itself or to start a mock server that the webClient can use as an endpoint?</p>