Mocking API response in Jmeter with WireMock - jmeter

Is it possible to mock the json response of an API in Jmeter using WireMock.
If yes, can you please share the step by step process for that.

Wiremock is a separate standalone Java application, you can run it as the different process like:
java -jar wiremock-jre8-standalone-x.xx.x.jar --port 8080
and once you add stub mappings using HTTP Request samplers or JSON configuration files the mock responses will be returned according to the stub mappings.
Wiremock can be launched using OS Process Sampler like:

Related

Start an embedded gRPC server while running integration tests with Springboot

I have built a Springboot application which has a gRPC server as one of its dependencies. While running the integration test, I am able to use embedded Kafka, embedd MySQL etc using TestContainers, but how can I create an embedded gRPC server while running my integration test suite?
I have the .proto contract definition file. I would use Wiremock to mock the request and responses to the gRPC server, but I am unable to start the embedded gRPC server yet.
It will be really great if I could find some help here.
Come to think of it, my question can be generalized to starting a generic embedded HTTP server in Springboot integration test, using TestContainers or otherwise.
I had a similar problem with IT testing Java gRPC service integrations, as using InProcessServer is quite cumbersome and quite hard to use when you need to return different responses for different tests. Also when using it you cannot really test your Stub beans and their configuration, since you can't pass InProcessServer host as a variable.
For this purpose I've created a gRPC Java mocking tool gRPC Mock. It has two integration modules - one for spring-boot and one for JUnit5. It follows a similar DSL type of structure to HTTP mocking service WireMock for creating the stubs. You can visit the GitHub page for some examples.

Stub for Feign client for integration testing

I have a spring cloud project with the following packaging structure
Controller (publishes Rest Endpoint)-->flow (business logic)-->service (calls Feign client with hysterix fallback setup )--> Feign client.
Auto-wiring is done in respective classes e.g. flow is auto-wired in controller and service is auto-wired in flow and so on.
I want to perform integration test, by calling the endpoint published by the controller. The problem is I don't have endpoint accessed by the feign client at the moment (neither original nor spring cloud contract stub is available).
How do I stub the call made by feign client in this case.
You can use Spring Cloud WireMock support and set up an endpoint manually before the tests are called. In the feign configuration you can point manually to an IP and port. The problem is that this test is pretty much useless cause as a consumer you're mocking the producer.
UPDATE
You have a Feign client that will be used to call some external API. What you can do is you can use Spring Cloud WireMock (or just WireMock) to setup a mock of that API. Then you can teach WireMock to behave as you wish and assert whether your client works fine. The problem with such an approach is such that since you, as a client, are setting up the WireMock instance, you can teach it to behave in the way that has nothing to do with the real API. For example you state that if you send a request to endpoint /foo with a method GET then you should get back "BAR" in the response. Then you write a test where your client sends GET # /foo and assert that BAR got properly returned. However that doesn't mean that the other API indeed has that endpoint. So this approach can give you false-positives. You can however use WireMock to assert whether you can properly react to faulty responses like malformed response etc.
In such cases, if you really want to check if you can communicate properly with an API that you don't control, is that you can write tests that will call that real API via a WireMock proxy, you record that traffic and convert it into stubs. You can watch about this more in my presentation here https://www.youtube.com/watch?v=ZyHG-VOzPZg

Not getting the API testing sampler in jmeter 3.2

I am using jmeter version 3.2 for REST API testing but i am not getting the API test request sampler in this version. Anyone who is working on the same version please let me know the option. Currently i am test the API through HTTP request sampler.
What do you mean by API test request? You should be able to test either SOAP or REST API endpoints using HTTP Request sampler, you will just need to add HTTP Header Manager to send Content-Type header, in your case it will most likely be application/json
References:
Building a SOAP WebService Test Plan
REST API Testing - How to Do it Right

Can we use Jmeter with Rest Assurred framework?

I need to know if we can implement Jmeter script with the RestAssured framework?
Theoretically yes, given you copy RestAssured .jar file along with dependencies to JMeter Classpath you should be able to use its methods from JSR223 Test Elements or reuse your existing tests via JUnit Request Sampler
However it would be much better to use JMeter's HTTP Request samplers as in this case you will be able to access networking metrics such as Connect Time and Latency, automatic failures on HTTP Status Codes above 399, Cookies, Headers, and Authorization management, etc.
See REST API Testing - How to Do it Right article to learn how to build an API test plan using JMeter

Send Mock response for Soap Request

We have created an soap request and sending the request using WebServiceTemplate
WebServiceTemplate.marshalSendAndReceive(request)
As my client is not ready with the server. I am trying to handle this request in my local and planning to post some dummy response.
Can you help me how I can mock this response?
If you are using CXF, see my JUnit Rule utility project. It creates an Endpoint using JaxWsServerFactoryBean.

Resources