3

I am trying to configure wiremock port. Its always giving the below exception. I am using the latest version 2.8.0 version

Exception in thread "main" 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: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:380)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:368)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:110)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:273)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:268)
at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:99)
at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:103)
at Testme.main(Testme.java:18)

Here is my code

 public static void main(String args[])
{
    System.out.println("Starting server");
    WireMockServer wireMockServer = new WireMockServer(8090);
    //Tried this one as well
    //options.port(8090)
    //WireMock.configureFor(8090);
    wireMockServer.start();

    stubFor(get(urlEqualTo("/my/resource"))
            .withHeader("Accept", equalTo("text/xml"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));
}
vkrams
  • 7,267
  • 17
  • 79
  • 129

4 Answers4

5

The static stubFor() method you are calling creates the stub on a default WireMock instance. You need to use your instance of WireMockServer when creating stubs.

wireMockServer.stubFor(...);
2

I use:

WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(PORT));

Did you already try it like that?

FISHET
  • 21
  • 4
  • 1
    this too gets the `Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused)` – ses Sep 30 '19 at 13:30
2

You also need to configure WireMock.

@BeforeAll
private static void init(){
    wireMockServer = new 
    WireMockServer(WireMockConfiguration.wireMockConfig().port(7070));
    wireMockServer.start();
    WireMock.configureFor("localhost", wireMockServer.port());

}
vinod chambole
  • 164
  • 1
  • 4
1

Current latest version 2.11.0. Following syntax applies

WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().port(8088)
vkrams
  • 7,267
  • 17
  • 79
  • 129
  • 1
    regardless I get: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused) – ses Sep 30 '19 at 13:29
  • Check idontevenseethecode's answer, https://stackoverflow.com/a/57795952/, it's how it should be done. – Chris Jul 22 '21 at 10:24