Spring: Mocking REST API Web Service

A tester often encounters the task of writing automated unit tests. The unique aspect of this type of testing is that it focuses on testing individual modules for compliance with specific requirements, rather than full integration. We are not concerned with third-party modules at this stage, and we can simulate expected responses from them. There are various approaches to addressing this challenge, ranging from utilizing existing Java libraries and frameworks like Mockito to creating custom mock stubs and deploying them on the application server.

Write a REST API web service using Spring Boot. Implement a GET method that returns a JSON message in the response. Also, implement a POST method with logic that accepts a JSON message as input, deserializes it, and generates a specific response based on the parameter values. Start the web service and verify its correctness by testing its functionality.


API RESTFull Json Spring Boot

Spring Boot Startes are handy for creating a project:

For the example POST request in JSON format:


Let’s add some logic to the response, if cardNumbers = 4*** on the incoming message, then the response should get:

       "desc":"Успешно обработано"

Well, and if cardNumbers != 4***, then in the answer:

       "desc":"Сообщение не обработано"

So the project involves 2 messages in JSON format. The next step is to describe POJO classes to work with them. To describe POJO I use lombok and jackson 2 libraries.

import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
 * @author Muflikhunov Roman
public class PhonesByCardNumberRq {
    private String rqUid;
    Messages MessagesObject;

Next, create the Controller class in which we implement the methods of our stub.

@RequestMapping(value = "/checkCard", method = RequestMethod.POST)
    public ResponseEntity checkCard(@RequestBody PhonesByCardNumberRq phonesByCardNumberRq){
        PhonesByCardNumberRs phonesByCardNumberRs = new PhonesByCardNumberRs();
        StatusRule statusRule = new StatusRule();
        statusRule.setDesc("Сообщение не обработано");
        if (phonesByCardNumberRq.getMessages().getCardNumbers().matches("^4.*")) {
            statusRule.setDesc("Успешно обработано");
        return new ResponseEntity<>(phonesByCardNumberRs, HttpStatus.OK);

Using ResponseEntity class which is a wrapper for the response and additionally for HTTP headers and status code, you can control everything that goes into it: status code, headers and body.

The task is to create a REST API web service using Spring Boot. It requires implementing a GET method to return a JSON response and a POST method to deserialize incoming JSON and generate a specific response based on the parameters. Testing the functionality ensures the service’s correctness. ResponseEntity allows control over the response, including status code, headers, and body.

Link to the full version of the project on GitHub:
Github SpringMockWebService

Related Posts