Spring boot Wiremock Integration
Demonstration of Running WiremockServer as Spring boot application, which helps in various test strategies such as integration, contract and performance testing.
What is Wiremock
It is a mock server
which enables you to stay productive with the following use cases:
-
API you depend on doesn’t exist
-
API you depend on is n’t complete.
-
It supports testing of edge cases and failure modes that the real API won’t reliably produce.
-
Because it’s fast it can reduce your build time from hours down to minutes.
Wiremock server can be useful in different test strategies
-
When you don’t have enough test data to do end-to-end testing with the new features you implemented.
-
When you want to verify your producer contracts while running build pipelines such as Consumer driven contract testing.
-
Performing load testing on your application components.
-
When you want to do Integration testing and you don’t have access to external services that your application talks to.
Wiremock server can be used in different ways to do testing ,refer wiremock guide
Running WireMock Server as SpringBoot application
Running as spring boot application has some advantages.
we can create separate mocks and place it inside our application, which acts as real server.
Consider a scenario where im trying to consumer online product catalog service with URL /product
which should return product information.
First create product_mapping.json as shown below
{
"request": {
"method": "GET",
"url": "/product"
},
"response": {
"status": 200,
"bodyFileName": "product.json"
}
}
and create product.json as a snapshot from the real service
{
"product": {
"id": 0,
"name": "string",
"description": "string",
"price": 0,
"discount": 0,
"weight": 1,
"stock": 100
}
}
place these two files under classpath/specific location.
Write simple configuration file to specify the location of your mocks with usingFilesUnderClasspath
() method.
package com.tvajjala.wiremock.config;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Wiremock configuration server for optimum performance
*
* @author ThirupathiReddy Vajjala
*/
@Configuration
public class WiremockConfig {
@Value("${wiremock.port}")
private int mockServerPort;
private static final Logger LOGGER = LoggerFactory.getLogger(WiremockConfig.class);
@Bean
public WireMockServer wireMockServer() {
WireMockConfiguration configuration = WireMockConfiguration.wireMockConfig()
// .bindAddress() //bindToSpecific IP address
.port(mockServerPort)
.preserveHostHeader(true)
//TODO: Typically it is only necessary to tweak these settings if you are doing performance testing under significant loads.
//Set the number of request handling threads in Jetty. Defaults to 10.
.containerThreads(100)
// Set the number of connection acceptor threads in Jetty. Defaults to 2.
.jettyAcceptors(10)
// Set the Jetty accept queue size. Defaults to Jetty's default of unbounded.
.jettyAcceptQueueSize(100)
// Set the size of Jetty's header buffer (to avoid exceptions when very large request headers are sent). Defaults to 8192.
.jettyHeaderBufferSize(16834)
.enableBrowserProxying(true)
//.notifier(new Slf4jNotifier(true))
.usingFilesUnderClasspath("src/main/resources/contracts");// can be read from property file but no benefit with that
LOGGER.info("Creating instance of WireMockServer with stubs location {} ", "/contracts");
return new WireMockServer(configuration);
}
}
Start your spring boot application, and access the endpoint http://localhost:8084/product which gives you product information.
While performing testing, replace your real service URL , Lets say http://online-shopping.com/product to http://localhost:8084/product
You can write more complex mocks based on your need. |
Other Interesting Features
start the start using my github repository source code |
-
Publish mocks with their REST endpoint , Refer http://localhost:8084/__admin/swagger-ui/
-
Live recording of your endpoint to use it for future , Refer http://localhost:8084/__admin/recorder/
-
Use DSL library to write Mock programmatically. Refer docs
-
Start in-memory server with Junit Rule
It is recommended to run wiremock as standalone approach, so that multiple application can be able access the mocks. |
Source
Refer my github repository for complete source code
Comments
Post a Comment