Wiremock: stubFor method fails since client is still configured to use port 8080 -

Created on 4 Mar 2016  路  6Comments  路  Source: tomakehurst/wiremock

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)

at

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

Most helpful comment

You need to do this: WireMock.configureFor("localhost", wireMockServer.port());

All 6 comments

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
Was this page helpful?
0 / 5 - 0 ratings

Related issues

karolzmuda picture karolzmuda  路  5Comments

rugden picture rugden  路  3Comments

wujimin picture wujimin  路  4Comments

samdnz picture samdnz  路  3Comments

leoluz picture leoluz  路  5Comments