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:
- return a “mock” response when a request matches an expectation
- return an invalid response or close the connection when a request matches an expectation
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:
- 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:
MockServer could be used to mock the service dependencies, as follows:
We will demonstrate this architecture by following approach.
- An simple CRUD Spring Boot application.
- 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.
- Spring boot app
- Java 8
- Mock Server
In the CRUD application, We have 2 endpoints for Add a Product and Get the Product Details.
Sample Add Product JSON :
Sample GET Products JSON :
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();
HttpEntity<String> entity = new HttpEntity<String>(gson.toJson(product), headers);
restTemplate.exchange(builder.toUriString(), 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
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