Can JMeter mock HTTP request
Asked Answered
I

3

5

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) ?

Innervate answered 6/3, 2018 at 11:38 Comment(0)
H
8

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);
    }
}
Heliograph answered 6/3, 2018 at 12:10 Comment(0)
R
2

From what I gather you would like to simulate a request for the purposes of the test results, while simultaneously sending a real request independent of your test results. One way you can accomplish this is to attach a JSR223 PostProcessor to a Dummy Sampler in this fashion:

JMeter JSR223 PostProcesssor element added under Dummy Sampler element

The PostProcessor element is added under the Dummy Sampler and executes after the simulated request. Inside the script for the JSR223 PostProcessor, we can add a few lines of scripting to execute a fire-and-forget request which satisfies your use case:

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpResponse;
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://www.your-domain.com");
HttpResponse response = httpClient.execute(request);
log.info("HTTP Response: " + response);

This script can be applied anywhere you want to make an HTTP call, but do not want the result of that call to impact your test results. You can simply change the following line to the actual URL you intend to call:

enter image description here

The last line containing log.info(...) is optional, but will output the result (i.e., status code 200, 404, etc.) for the request in the test logs which may be useful for debugging purposes.

Romans answered 18/10, 2023 at 7:52 Comment(1)
Thank you, I also don't want to wait for responseInnervate
I
0

I manged to do it using previous post (https://mcmap.net/q/1944521/-can-jmeter-mock-http-request), but succeed only after putting it in "bzm - Parallel Controller" and setting timeout to define time of WireMockServer's work: image example

Inboard answered 27/8, 2019 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.