Search code examples
javastubwiremock

Unable to set wiremock server port


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

Solution

  • Current latest version 2.11.0. Following syntax applies

    WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().port(8088)