Popular Posts

Spring boot Wiremock Integration

What is Wiremock

Demonstration of Running WiremockServer as Spring boot application, which helps in various test strategies such as integration, contract and performance testing.

— ThirupathiReddy Vajjala

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

  1. When you don’t have enough test data to do end-to-end testing with the new features you implemented.

  2. When you want to verify your producer contracts while running build pipelines such as Consumer driven contract testing.

  3. Performing load testing on your application components.

  4. 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

product_mapping.json
{
  "request": {
    "method": "GET",
    "url": "/product"
  },
  "response": {
    "status": 200,
    "bodyFileName": "product.json"
  }
}

and create product.json as a snapshot from the real service

product.json
{
  "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.

WiremockConfig.java
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
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

No comments:

Post a Comment