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 ;)
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.