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.

Assignment:
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.

Used:

API RESTFull Json Spring Boot

Spring Boot Startes are handy for creating a project:

For the example POST request in JSON format:

{
 "rqUid":"abdfabfa68466a5aef768fadd4223333",
 "messages":{
   "id":"abcd1111abcd1111abcd1111abcd2222",
   "type":"WAY4-A",
   "cardNumbers":"4444000022221111"
 }
}

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

{
    "value":"4444000022221111",
    "statusRule":{
       "code":"SUCCESS",
       "desc":"Успешно обработано"
    },
    "timeout":10
}

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

{
    "value":"4444000022221111",
    "statusRule":{
       "code":"FAIL",
       "desc":"Сообщение не обработано"
    },
    "timeout":10
}

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
 */
 
@Getter
@Setter
@ToString
public class PhonesByCardNumberRq {
    @JsonView
    private String rqUid;
    @JsonView
    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();
        phonesByCardNumberRs.setValue(phonesByCardNumberRq.getMessages().getCardNumbers());
        StatusRule statusRule = new StatusRule();
        statusRule.setCode("FAIL");
        statusRule.setDesc("Сообщение не обработано");
        phonesByCardNumberRs.setStatusRuleObject(statusRule);
        phonesByCardNumberRs.setTimeout(10L);
 
        if (phonesByCardNumberRq.getMessages().getCardNumbers().matches("^4.*")) {
            statusRule.setCode("SUCCESS");
            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