Search code examples
jmeterjmeter-plugins

Can JMeter mock HTTP request


I want to Mock HTTP requests, meaning sending real request to real server, but ignore (not wait) and override the response with a dummy response,

JMeter have many tools which are close but not enough,

DummySampler plugin is close but not really sending request,

An old answer direct to Mirror Server which seems irrelevant for specific API requests and responses.

JMeter does not simulate servers.

Having said that, JMeter 2.3 has a built-in mirror server - it accepts any HTTP request and responds with a page containing the request details.

If server B does not care what server C sends back, then you could use this to "mock" server C.

My answer on ignoring HTTP response by adding Runtime controller with 1 second and updating the response data is a problematic workaround but can work.

Is there a better option available in plugins or executing some other tool in parallel?

Is opening an enhancement for JMeter is relevant and if so, should it improve HTTP Request or is it a new sampler as Mock HTTP Request? can Runtime controller support only sending and stop waiting for response (by using 0 seconds for example) ?


Solution

  • The easiest option would be going for i.e. WireMock which is extremely powerful and flexible.

    You can integrate it with JMeter by adding WireMock jar (along with dependencies) to JMeter Classpath and running the WireMockServer from the JSR223 Test Elements using Groovy language.

    If you're not too comfortable with Groovy you can run WireMock as a standalone Java application using OS Process Sampler


    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("0.0.0.0", 8080);
            wireMockServer.start();
            StubMapping foo = stubFor(get(urlEqualTo("/wiretest"))
                    .willReturn(aResponse()
                            .withStatus(200)
                            .withBody("Hello World")));
            wireMockServer.addStubMapping(foo);
        }
    }