How to verify a wiremock service get some calls

1.2k Views Asked by At

I am doing a functional testing of a service A. The function is triggered by an api call 1 from service B and then at last respond (send another api call 2) to the service B. Therefore, the plan is to wiremock the service B and make a call 1 and then verify that it get a api call 2 request back.

We use wiremock in the docker compose file to simulate the service B as below:

service-b:
    image: ***/wiremock-service-b
    ports:
      - "8081:8081"
    volumes:
      - ./wiremock/service-b/mappings/:/home/wiremock/mappings/
      

Is there any way I can get THIS service B wiremock instance (deployed in docker) using java code and triggered a call 1 and then verify a call 2 request coming back? Please help. Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

If the docker compose is running, you can configure the default static WireMock client to use it as so:

import static com.github.tomakehurst.wiremock.client.WireMock.*;

configureFor("localhost", 8081);

as you are port forwarding to 8081.

See WireMock > Java configuration for further details.

You can then verify that service B was called as usual:

verify(postRequestedFor(urlEqualTo("/verify/this"))
        .withHeader("Content-Type", equalTo("text/xml")));

See Wiremock > Verifying for further details.

2
On

It seems like you are mixing two approaches on your tests. I assume you are doing an integration test:

  1. When using Wiremock, you don't need to launch your service in a Docker container. The purpose of Wiremock, is to simulate the response you receive when you call the service you are mocking. You don't really call the service in this case.
  2. With the second approach you can use a Docker container with TestContainer. In this case, to check whether the service is called you have to test the response and check if this is your expected results. First the service (in your container) you want to call have be to exposed by an API, then in your service you are testing, you call the service with TestRestemplate or WebTestClient.

A sample code will be like this:

@Test
void integrationTest_For_Status_200() {
    webTestClient.post()
            .uri("THE_SERVICE_URL")
            .accept(MediaType.APPLICATION_JSON)
            .bodyValue(personDto)
            .exchange()
            .expectStatus().isOk()
            .expectBody(Response.class)
            .value(response -> assertAll(
                    () -> assertThat(response.getStatusValue()).isEqualTo(HttpStatus.OK.value()),
                    () -> assertThat(response.getStatusReasonPhrase()).isEqualTo(HttpStatus.OK.getReasonPhrase()),
                    () -> assertThat(response.getDescription()).isEqualTo(category.getDescription())
            ));
}

I would suggest those links bellow that will explain the approach of integration tests: https://rieckpil.de/guide-to-springboottest-for-spring-boot-integration-tests/ https://www.freecodecamp.org/news/integration-testing-using-junit-5-testcontainers-with-springboot-example/ https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#webtestclient-tests