2

I'm having a problem when I configure a WireMockServer inside a Cucumber with testng v7.0.0 (I think that's irrelevant here). I want to call a service of my own, which per se requests the url I'm defining.

When I try to invoke like this:

@When("the service is called")
public void theServiceIsCalled() {
    wireMockServer = new WireMockServer(options().port(PORT_NUMBER));
    wireMockServer.start();
    configureFor("127.0.0.1", wireMockServer.port());
    stubFor(get(urlEqualTo(url))
                .willReturn(aResponse()
                        .withStatus(HTTPOK)
                        .withHeader("Content-Type", "application/json")
                        .withBody(response)));
    myService.callService();
}

I always get

java.util.concurrent.CompletionException: javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:9090 [/127.0.0.1] failed: Connection refused

I know this can't be a problem of a wrongly defined port, url or headers. I say that because when I run a java instance with the exact same WireMock configurations (without the service call), and then the test, it runs smoothly, the service returns in fact what I have mocked. It seems like the WireMockServer lifetime must reside in a process outside of the one I want.

What's going on?

Thanks in advance ;)

2
  • 1
    Have you tried setting a breakpoint and checking that WireMock is running just before getting the Exception? You can see which (if any) mocks are being served at <wiremock url>/_admin
    – Marit
    Commented Nov 26, 2019 at 8:29
  • 1
    Thank you @Marit for your answer. No, it seems that wiremock shuts down as soon as I call callService(). The browser also returns a Connection Refused when I set a breakpoint within callService().
    – FrVentura
    Commented Nov 26, 2019 at 8:39

1 Answer 1

8

So, it seems that the problem was due to 2 causes:

1 - I wasn't actually initializing the wireMockServer (initialization was being held in a BeforeAll method which was not actually being called), sorry about that.

2 - The stubFor must be used against the wireMockServer instance.

Below is a working configuration:

WireMockServer server = new WireMockServer(options().bindAddress("127.0.0.1").port(port));
server.stubFor(get(urlEqualTo(urlWithId))
                .willReturn(aResponse()
                        .withStatus(HTTPOK)
                        .withHeader("Content-Type", "application/json")
                        .withBody(response)));
server.start()
myService.callService();

Hope someone finds this useful.

1
  • 2
    The stubFor point was really helpful. The documentation never states it - weird! was scratching my head all day. Commented Oct 19, 2020 at 18:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.