0

What the below code does is that it gets items for specific(using client1) customer and delete them by making another endpoint, client2. The first endpoint provides pagination.

Now, I am having issue with testing it. As you can see, I am using wiremock but it does not seem working properly as the last endpoints never called. I have provided the response body for the first stub, was expecting the next endpoints to be triggered. I tried to debugging by putting break point to see if they are called, but the code never reach the section where the two endpoint are triggered. I am not sure the way I did is correct. Is this the correct approach to test using wiremock?

public void deleteItems(String id) {
    client1.get()
        .uri(urlBuilder -> urlBuilder
            .path(CLIENT_ONE_ENDPOINT)
            .queryParam("email", id)
            .build())
        .retrieve()
        .bodyToMono(Items.class)
        .expand(items -> {
            while (!items.isOnLastPage()) {
                int nextPage = items.getCurrentPage() + 1;
                return client1.get()
                    .uri(builder -> builder
                        .path(CLIENT_ONE_ENDPOINT)
                        .queryParam("email", id)
                        .queryParam("pageNumber", nextPage)
                        .build())
                    .retrieve()
                    .bodyToMono(Items.class);
            }
            return Flux.empty();
        })
        .flatMapIterable(Items::getResults)
        .map(or -> {
            return client2.delete()
                .uri(CLIENT_TWO_ENDPOINT + or.getId())
                .retrieve()
                .bodyToMono(ResponseEntity.class)
                .subscribe();
        })
        .subscribe();
}

wiremock test

@Test
void itemsAreRemoved() {
    String email = "example@example.com";
    String itemId = "001";

    WireMock.configureFor(PORT);
    wireMockServer.stubFor(get(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
        .withQueryParam("email", equalTo(email))
        .withHeader("Content-Type", equalTo("application/json"))
        .willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withBody(loadResponse(itemId))
            .withStatus(200)));
    wireMockServer.stubFor(get(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
        .withQueryParam("email", equalTo(email))
        .withQueryParam("pageNumber", equalTo("1"))
        .withHeader("Content-Type", equalTo("application/json"))
        .willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withStatus(200)));
    wireMockServer.stubFor(delete(urlPathEqualTo(CLIENT_TWO_ENDPOINT + itemId)));

    service.deleteItems(email);

    verify(getRequestedFor(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
        .withQueryParam("email", equalTo(email)));
    verify(getRequestedFor(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
        .withQueryParam("email", equalTo(email))
        .withQueryParam("pageNumber", equalTo("1")));
    verify(exactly(1), deleteRequestedFor(urlPathEqualTo(CLIENT_TWO_ENDPOINT + itemId)));

}
Minimum
  • 21
  • 3
  • Does CLIENT_TWO_ENDPOINT include the /orders/ path part? If not, this might be the issue. – Tom Jan 23 '23 at 15:09
  • I have updated it to make it clear – Minimum Jan 23 '23 at 16:19
  • It looks like it should work from what you've shown. If the verify is failing though, I suggest calling getAllServeEvents() and eyeballing it to see if there's a DELETE event in there with something not quite matching. – Tom Jan 25 '23 at 14:07
  • Does this answer your question? [How to unit test the WebClient.onStatus() in java?](https://stackoverflow.com/questions/75114698/how-to-unit-test-the-webclient-onstatus-in-java) – Alex Jan 27 '23 at 21:22

0 Answers0