0

I have 3 microservices Want to mock last ms api response.

Ex: my 1st api sends the correct request. And some fields are used in the 2nd MS API to send the request to 3rd MS API.

Now I want to send 403 from the 3rd MS api.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 06 '22 at 08:52

1 Answers1

0

You can use Wiremock for this case. It's a library for stubbing and mocking web services.

This is the example of basic usage:

int port = 8080;
WireMockServer wireMockServer = new WireMockServer(port);
wireMockServer.start();

stubFor(get(urlEqualTo("/ms/api/v3"))
        .willReturn(aResponse()
                .withStatus(HttpStatus.FORBIDDEN.value())));

// after completing all your tests
wireMockServer.shutdown();

Firstly, you configure your WireMock server. Then you specify the stub for a specific URL. After all the work you should also shut down the server.

If you're using the Spring framework and JUnit, it might be a good idea to set up the WireMock server as a Spring-manged bean. This will remove a lot of overhead. Please see my configuration example below:

import com.github.tomakehurst.wiremock.WireMockServer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;

@TestConfiguration
public class WiremockConfig {
    private static final Integer WIREMOCK_PORT = 8080;

    @Bean(initMethod = "start", destroyMethod = "stop")
    public WireMockServer wireMockServer() {
        return new WireMockServer(WIREMOCK_PORT);
    }
}

Dependencies you may need:

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock</artifactId>
    <version>2.27.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8-standalone</artifactId>
    <version>2.32.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>de.mkammerer.wiremock-junit5</groupId>
    <artifactId>wiremock-junit5</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>
  • Hi Alex, Thanks for the reply. i am already using wiremock for mocking the response in my code. My problem is "I have 3 microservices. This is how all ms works: 1st ms api request --> 2nd ms --> 3rd ms) and the response will be like: 3rd ms reaponse --> 2nd ms reaponse--> 1st ms. Now my scenarios is 1 want to send the request from 1st ms and configure the 3rd ms response according to my use. Ex: 3rd microservice is not responding or taking time to respond (timeout scenario). So how 1st ms will behaive. – Ankur Verma Sep 07 '22 at 09:24