22

I have this piece of code in a Junit, where I clearly set the port to 8888

when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap()))
                .thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods");

stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(
                aResponse().withStatus(200)
                        .withHeader("Content-Type", APPLICATION_JSON_VALUE)
                        .withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));

but when I run the test I got this error on this line:

stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(..

and the error:

wiremock.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
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80

4 Answers4

20

For Java users

Based on the WireMock docs.

There are 3 possibilities to use WireMock in your tests :

  1. If you are using Wiremock as JUnit 4 rule to configure the port use :
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

...

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8888));
  1. If you are using new instance and start it from your Test class (for example @Before) :
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

...

public class Test {

    WireMockServer wm;

    @BeforeEach
    void setUp() {
        wm = new WireMockServer(options().port(8888));
        wm.start();
    }

    @Test
    void test() {
        wm.stubFor(...);
    }
}
  1. With static configuration of default instance (not using new instance in your test) :
WireMock.configureFor(8888);

For Kotlin users

If you are using kotlin you can add actual wiremock instance to stubFor and verify calls like wm.stubFor() and configure the port like in option 3 of this answer.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • 2
    hmm still: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused). unit 5 – ses Sep 30 '19 at 13:19
  • 23
    hi @ses i recently just figured this out. i am using Kotlin and JUnit 5, but had that `HttpHostConnectException` issue. I resolved it by adding the actual `WireMockServer` instance to my `stubFor` or `verify` methods, i.e. `wireMockServer.stubFor()` or `wireMockServer.verify()`. After doing this, the tests work. Hope it helps. – billydh Jan 30 '20 at 01:55
  • 2
    For completeness, it's worth mentioning that you might have to also start the server. I went with option 2. with `new WireMockServer()` but forgot to call `wm.start()` and was getting `Connection refused` – Anly Jun 27 '21 at 15:35
  • @billydh, you should add your comment as an answer. It worked for me, and I'm sure a lot of other people too. Thanks! – jmrah Jun 09 '22 at 23:52
8

Posting my previous comment as an answer as it seems to have helped a few people. Thanks @jmrah. :)

For Kotlin and JUnit5, this can be resolved by adding the actual WireMockServer instance to the stubFor or verify method calls.

wireMockServer.stubFor()

or

wireMockServer.verify()

After adding this, the tests should work.

billydh
  • 975
  • 11
  • 27
0

You can also use this: @AutoConfigureWireMock(port = 0, httpsPort = xxxx)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '23 at 10:50
0

You need to create a WireMockServer object and assign it to a variable say wm, then use this variable with the method like so wm.stubFor(...). If you don't use the wm variable, you will get the error connection refused.

tarekahf
  • 738
  • 1
  • 16
  • 42