0

I am trying to get simple stubbing with Wiremock (standalone) in Cucumber Java with RestAssured.

I have the below code in stepdefinition class

    protected final int PORT = 8089;

    @Rule
    public WireMockRule wiremockRule = new WireMockRule(PORT);


    @Given("^mock environment has been setup$")
    public void mockEnvironmentHasBeenSetup() {

        stubFor(any(anyUrl()).willReturn(ok()));
    }

But it throws and below error in the above method

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
    at wiremock.org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)

As per below solution it seems like common error but can happen due to several reasons.

Sequence Issue
Incorrect call

POM.xml

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8</artifactId>
    <version>2.26.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>

In my case not quite sure what's happening.

Second Approach

I am using junit-jupiter-engine - 5.5.2 with wiremock 2.6.0

WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089));

        wireMockServer.start();
        int port = wireMockServer.port();
        wireMockServer.stubFor(any(anyUrl()).willReturn(ok()));
        System.out.println (port);
Shabar
  • 2,617
  • 11
  • 57
  • 98
  • After removing `stubFor(any(anyUrl()).willReturn(ok()));` and placed the wiremock request response json under `resources` folder the above said error disappeared but now I can't see ant `wiremcok` server running on `port 8089` – Shabar Feb 09 '20 at 12:04
  • The other question was should I need to `import com.github.tomakehurst.wiremock.client.WireMock;` here for `JUnit4.X `. I have added `POM ` entries to the question. But still cannot use the `wiremock.client` – Shabar Feb 09 '20 at 23:48
  • Cucumber doesn't support JUnit rules (it's not JUnit). You have to start the WireMockServer in a Before hook and stop it in an After hook. – M.P. Korstanje Feb 10 '20 at 06:46
  • @mpkorstanje As suggested I updated the code to `second approach` in the question. So still when I hit `http://localhost:8089/__admin/docs/` endpoint after startup the wiremock server. it keep loading without any page load. I can see the port allocated though (i.e. 8089). So I assumed wiremock has not been loaded properly yet. – Shabar Feb 11 '20 at 01:35

0 Answers0