In the previous tutorial, I explained about the Testing of SpringBoot GET Method. In this tutorial, I will discuss about the Testing of POST method which create Resource in SpringBoot application.
First, let us see what are RequestBody and PathMapping annotations which are commonly used assnotations for POST Method
1. What is RequestBody
The @RequestBody annotation maps body of the web request to the method parameter. The body of the request is passed through an HttpMessageConverter
to resolve the method argument depending on the content type of the request.
public ResponseEntity<Object> createStudent(@Valid @RequestBody Student student) {
2. What is PathMapping
The @PathMapping annotation is the specialized version of the @RequestMapping annotation which acts as a shortcut for @RequestMapping(method=RequestMethod=POST).
Here, we will use @PostMapping. It is annotation for mapping HTTP POST
requests onto specific handler.
@PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method=RequestMethod.POST)
@PostMapping("/students")
The dependencies need in POM.xml to test springboot framework is mentioned here.
Code of StudentController.java is below
@PostMapping("/students")
public ResponseEntity<Object> createStudent(@Valid @RequestBody Student student) {
Student savedStudent = studentRepository.save(student);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedStudent.getId()).toUri();
return ResponseEntity.created(location).build();
}
ResponseEntity.created(location).build(): Return a status of created. Also return the location of created resource as a Response Header.
Code of Student.java is below
@Entity
public class Student {
@Id
@GeneratedValue
private Long id;
@NotNull
@Size(min = 4, message = "Name should have atleast 4 characters")
private String name;
private String passportNumber;
public Student() {
super();
}
public Student(Long id, String name, String passportNumber) {
super();
this.id = id;
this.name = name;
this.passportNumber = passportNumber;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
}
This is the Student entity. Each entity must have at least two annotations defined: @Entity and @Id. The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping. The @Id annotation specifies the primary key of an entity and the @GeneratedValue provides for the specification of generation strategies for the values of primary keys.
Here, Student class is annotated with @Entity, indicating that it is a JPA entity. (Because no @Table annotation exists, it is assumed that this entity is mapped to a table named Student .)
The Student object’s id
property is annotated with @Id so that JPA recognizes it as the object’s ID. The id property is also annotated with @GeneratedValue to indicate that the ID should be generated automatically.
Scenario 1- Below picture shows how we can execute a sucessful POST Request Method on a Resource using Postman
In the below image, a new Student with name “Mat” is created and the status code returned is 201.

In the below image, I’m checking if the new user is created with name “Matt”. As id is auto genertaed, so the ID generation starts from 1.

Above scenario can be tested in the below way.
Feature: Create Student Request
@CreateStudent
Scenario: Send a valid Request to create a student
Given I send a request to the URL "/students" to create a user with name "Matt" and passport as "RA000001"
Then the response will return status of 201 for new student
And I send a request to the URL "/students/1" to get detail of new student name as "Matt"
Test Code to test above scenario (StepDefinition file)
@SpringBootTest(classes = SpringBoot2RestServiceApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CreateStudentDefinition {
private final static String BASE_URI = "http://localhost";
@LocalServerPort
private int port;
private ValidatableResponse validatableResponse, validatableResponse1;
private void configureRestAssured() {
RestAssured.baseURI = BASE_URI;
RestAssured.port = port;
}
protected RequestSpecification getAnonymousRequest() throws NoSuchAlgorithmException {
configureRestAssured();
return given();
}
@Given("^I send a request to the URL \"([^\"]*)\" to create a user with name \"([^\"]*)\" and passport as \"([^\"]*)\"$")
public void iSendARequest(String endpoint, String newName, String passport) throws Throwable {
JSONObject newStudent = new JSONObject();
newStudent.put("name", newName);
newStudent.put("passport", passport);
validatableResponse = getAnonymousRequest().contentType(ContentType.JSON).body(newStudent.toString()).when()
.post(endpoint).then();
}
@Then("^the response will return status of (\\d+) for new student$")
public void extractResponseOfNewStudent(int status) {
validatableResponse.assertThat().statusCode(equalTo(status));
}
@And("^I send a request to the URL \"([^\"]*)\" to get detail of new student name as \"([^\"]*)\"$")
public void extractResponseOfNewStudent(String endpoint, String newName) throws NoSuchAlgorithmException {
validatableResponse1 = getAnonymousRequest().contentType(ContentType.JSON).when().get(endpoint).then();
validatableResponse1.assertThat().body("name", equalTo(newName));
}

Here, I have used JSONObject from org.json to create the JSON directly. I use the put() method and supply the key and value as an argument.
JSONObject newStudent = new JSONObject();
newStudent.put("name", newName);
newStudent.put("passport", passport);
There is another way to create a JSON using Map. Here we construct a custom Map and then pass it as an argument to JSONObject‘s constructor.
Map<String, String> map = new HashMap<>();
map.put("name", newName);
map.put("passport", passport);
JSONObject newStudent = new JSONObject(map);
For assertion purpose, we use Hamcrest Matchers. Hamcrest is a framework for software tests. Hamcrest allows checking for conditions in your code via existing matchers classes. It also allows you to define your custom matcher implementations.
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
The next tutorial explains about the Testing of PUT method in SpringBoot Application.
Very informative tutorial
LikeLike