Performing Integration Tests on REST Services using Mock Server and Expectations for Spring Boot Applications

Mock Server can be used for mocking any system you integrate with via HTTP or HTTPS (i.e. services, web sites, etc).

When Mock Server receives a requests it matches the request against active expectations that have been configured.

An expectations defines the action that is taken, for example, a response could be returned.

MockServer supports the follow actions:

MockServer allows you to mock any server or service that you connect to over HTTP or HTTPS, such as a REST or RPC service.

This is useful in the following scenarios:

  • testing
    • easily recreate all types of responses for HTTP dependencies such as REST or RPC services to test applications easily and affectively
    • easily setup mock responses independently for each test to ensure test data is encapsulated with each test. Avoid sharing data between tests that is difficult to manage and maintain and risks tests infecting each other
    • create test assertions that verify the requests the system-under-test has sent

Mocking Dependencies & Verifying Request

A system with service dependencies, as follows:

System In Production

MockServer could be used to mock the service dependencies, as follows:

Mocking service dependencies with MockServer

We will demonstrate this architecture by following approach.

  1. An simple CRUD Spring Boot application.
  2. An Consumer Service to using the above application.

We will create expectations class and methods in the consumer service to mock the requests and responses which we need from the CRUD Spring boot application.

Technical Specs:

  • Spring boot app
  • Java 8
  • H2
  • Mock Server
  • Junits

Implementations :

In the CRUD application, We have 2 endpoints for Add a Product and Get the Product Details.

Sample Add Product JSON :

“productName”: “Test1”,
“productType”: “Test1”

Sample GET Products JSON :

“productId”: “1”,
“productName”: “Test”,
“productType”: “Test”,
“port”: 8000,
“links”: [{
“rel”: “self”,
“href”: “http://localhost:8000/db/getproduct/1”
“productId”: “2”,
“productName”: “Test1”,
“productType”: “Test1”,
“port”: 8000,
“links”: [{
“rel”: “self”,
“href”: “http://localhost:8000/db/getproduct/2”

In the Consumer Service, We have the following simple REST Template Invocation for Adding and Getting Products.

Getting Product Details

productsObject = (Object) restTemplate
.exchange(builder.toUriString(), HttpMethod.GET, httpEntity, String.class).getBody();

Adding Product

HttpEntity<String> entity = new HttpEntity<String>(gson.toJson(product), headers);, HttpMethod.POST, entity, String.class);


The Above Implementations  are enough to consume the endpoints of the CRUD applications to suite our requirements.

MOCK Server Usage and Unit Tests:

The main benefit is that the mock server is operated and maintained for the benefit of the development team, so you can make it as simple or as complex as you need it to.

We need to implement the following in the Consumer Service to Mock the Request / Responses

Initializing Mock Server


We can externalize the PORT Number and other URL details and inject them while executing the test suites

Creating Expectations

We need to add the following Code in the Static Methods for Adding and Getting Products


The Above Codes helps setting the mock server Specifications and also helps to understand the what consumers should expect in case of Integration Tests.

Implementing Test Cases


In the Above Way, We can bypass the Rest Calls to be CRUD applications and do the integration tests in the Consumer Service.

Github Repository for the codes:


We have learned:

  • Why, What, How of mock server
  • Integrating Junits with Mock Server
  • Spring Boot Test Cases with Rest Template
Digiprove sealCopyright secured by Digiprove © 2019 Geeks 18

Be the first to comment

Leave a Reply

Your email address will not be published.