Warm tip: This article is reproduced from stackoverflow.com, please click
jmeter wiremock

Localhost connection refused when integrating WireMock and JMeter

发布于 2020-05-04 04:31:32

I'm trying to integrate Wiremock into a Jmeter test plan so that every time I execute the test plan it will start up an instance of WireMock in the beginning and then run tests I have outlined. I followed this answer (https://stackoverflow.com/a/49130518/12912945) but the problem I am having is that I always get the error:

Response message:Non HTTP response message: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

From what I can see, the Wiremock server never starts even though I have the following code in a JSR223 Sampler at the beginning of the test plan:

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class WireMockTest {

    public static void main(String[] args) {
        WireMockServer wireMockServer = new WireMockServer();
        configureFor("127.0.0.1", 8080);
        wireMockServer.start();
        StubMapping foo = stubFor(get(urlEqualTo("/some/thing"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("Hello World")));
        wireMockServer.addStubMapping(foo);
    }
}

Could anyone point me in the right direction of how to correctly integrate the two, I have tried adding to the classpath but I feel like I have not done this correctly or I'm missing something

Thank you!

Questioner
durol
Viewed
37
Dmitri T 2020-02-17 22:54

You're defining the main function but I failed to see where you're executing it. In other words your Wiremock initialization code isn't getting executed at all.

You need to explicitly call this main function in order to get your code executed, to get this done add the next line to the end of your script:

WireMockTest.main()

Once done the JSR223 Sampler will invoke the code inside the main function and the Wiremock server will be started.

Another option is to remove these class and function declarations and just use

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

WireMockServer wireMockServer = new WireMockServer();
configureFor("127.0.0.1", 8080);
wireMockServer.start();
StubMapping foo = stubFor(get(urlEqualTo("/some/thing"))
      .willReturn(aResponse()
              .withStatus(200)
              .withBody("Hello World")));
wireMockServer.addStubMapping(foo); 

as the script you define in the JSR223 Test elements don't require an entry point

Check out Apache Groovy - Why and How You Should Use It article for more information about Groovy scripting in JMeter tests.