How to stub/mock the downstream API's in Jmeter

21 Views Asked by At
  • I am new to the Jmeter tool , I see in some of the stack overflow pages that we can actually mock/stub using wiremock or something , but no actual documention provided anywhere*

Any info to get started is really helpful

I tried installing Jmeter and used the option mirror service which didn't worked.

1

There are 1 best solutions below

0
Ivan G On

JMeter doesn't provide any mocking capabilities per se. The only built-in test element which can be used is HTTP Mirror Server which basically returns received requests so it it not a real mocking solution.

It is possible to use i.e. JSR223 Sampler and write your own mocking server in Groovy, see [A simple web server written in Groovy that provides static and code-generated content]4 for example code

import com.sun.net.httpserver.HttpServer
int PORT = 8080
HttpServer.create(new InetSocketAddress(PORT), /*max backlog*/ 0).with {
    println "Server is listening on ${PORT}, hit Ctrl+C to exit."    
    createContext("/") { http ->
        http.responseHeaders.add("Content-type", "text/plain")
        http.sendResponseHeaders(200, 0)
        http.responseBody.withWriter { out ->
            out << "Hello ${http.remoteAddress.hostName}!\n"
        }
        println "Hit from Host: ${http.remoteAddress.hostName} on port: ${http.remoteAddress.holder.port}"
    }
    start()
}

WireMock is a separate project which is not connected to JMeter by any means. You can launch it and configure the mapping stubs according to your needs.

If you need to have possibility to launch Wiremock and configure it using JMeter test Wiremock provides REST API and Java API but to get a comprehensive answer you need to explain your requirement in details