0

I am trying to do code coverage for my below WebClient implementation. I was able to come up with a test case, but for some reasons it's not covering ExchangeToMono part wherein I am calling a private method. It's covering the other parts leaving the ExchangeToMono.

Service Layer

public void invokeEmailApi(String requestPayload)

    Mono<String> emailApiResponse =
        builder
            .baseUrl(emailBaseUrl)
            .build()
            .post()
            .uri(emailEndpoint)
            .contentType(MediaType.APPLICATION_JSON)
            .headers(
                httpHeaders -> httpHeaders.add("requestType", MediaType.APPLICATION_JSON_VALUE))
            .body(Mono.just(requestPayload), String.class)
            .exchangeToMono(this::handleResponse)
            .doOnSuccess(x -> log.info("Rest Call is Successful :: {}", x))
            .doOnError(x -> log.error("Rest Call failed :: ", x));

    emailApiResponse.subscribe(System.out::println);
  }

   private Mono<String> handleResponse(ClientResponse clientResponse) {
    if (clientResponse.statusCode().is5xxServerError()) {
      return clientResponse
          .bodyToMono(String.class)
          .flatMap(
              response ->
                  Mono.error(
                      new Exception("Rest Call Failed!!")));
    }
    return clientResponse.bodyToMono(String.class);
  }


Test Case

class EmailServiceImplTest {

  private static EmailServiceImpl emailService;

  private static MockWebServer mockWebServer;

  private static WebClient.Builder webClient;

  @BeforeAll
  static void setUpClass() throws IOException {
    mockWebServer = new MockWebServer();
    mockWebServer.start();
  }

  @AfterAll
  static void tearDown() throws IOException {
    mockWebServer.shutdown();
  }

  @BeforeEach
  void init() {
    emailService = new EmailServiceImpl(WebClient.builder(), mockWebServer.url("/").toString());
  }

  @Test
  void invokeEmailApi_2xx_Success() throws InterruptedException {
    mockWebServer.enqueue(
        new MockResponse()
            .setResponseCode(200)
            .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .setHeader("requestType", MediaType.APPLICATION_JSON_VALUE)
            .setBody("{\"transformToJson\": \"true\", \"inputFormat\": \"\"}"));

    emailService.invokeEmailApi("{\"transformToJson\": \"true\", \"inputFormat\": \"\"}");

    // mockWebServer.takeRequest();
  }

  @Test
  void invokeEmailApi_5xx_Failure() throws InterruptedException {

    mockWebServer.enqueue(
        new MockResponse()
            .setResponseCode(500)
            .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .setHeader("requestType", MediaType.TEXT_HTML_VALUE)
            .setBody("HTTP 500"));

    emailService.invokeEmailApi("{\"transformToJson\": \"true\", \"inputFormat\": \"\"}");
  }
}

Any idea how can the ExchangeToMono be covered using MockWebServer?

Jestino Sam
  • 526
  • 2
  • 12
  • 31

0 Answers0