Question

I want to test a proxy which will redirect calls to another service, and I'm trying to mock the destination service. For that, I would like to use wiremock, but the class using it would be a standalone class, with no tests in it.

I know that I can use the WireMockServer for that purpose, but I can't find an example on how to programmatically hook a handler to that server.

An example of my class would be:

public class MockedService {

    WireMockServer wireMockServer;

    public MockedService(int port) {
        wireMockServer = new WireMockServer(port);

    get(urlMatching("/process/?param1=.*&param2=.*"))
        .willReturn(aResponse()
        .withBody(GenericMediaResponse.RESPONSE_OK.toJSONString()));
    }

    public int getPort() {
        return port;
    }   
}

but again I don't know how to hook the get request to the server. Thanks.

Was it helpful?

Solution

I ran into same problem. After digging through the source code, here is what you need to do: You need to execute this statement before stubbing:

WireMock.configureFor("localhost",port);

This will set the default instance to bind to given host and port.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top