stubFor method fails with error when Wiremock Server started from within JAVA [ No Junit ]
All i am trying to do is start Wiremock Server in my java TestNG test that basically starts the server on port 8091. That fails and gives following error
Errror
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
Note that as a stand alone server, i am able to start the wiremock server on my machine via terminal just fine.
JAVA code
port=8091;
wireMockServer = new WireMockServer( wireMockConfig().port(port));
stubFor(get(urlEqualTo("/an/endpoint"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withStatus(200)
.withBody("You've reached a valid WireMock endpoint")));
You need to do this: WireMock.configureFor("localhost", wireMockServer.port());
Thanks
Works for me too. Thanks a lot.
Do you know why we need to add this? I can't find it in the docs.
If you are using spring boot. You can use this annotation on top of your class
@AutoConfigureWireMock(port = 8080)
you need to use your wiremokc rule for stubbing:
wireMockRule.stubFor(get(urlEqualTo("/an/endpoint")) ......
in my unit test i had the same problem. for me this fixed it:
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults
Most helpful comment
You need to do this:
WireMock.configureFor("localhost", wireMockServer.port());