How To Unit Test A POST REST Service Using Mockito With JUnit
Posted By : Kaushlendra Kumar Pal | 27-May-2018
In this blog of unit test, we will learn to post the request body to the specific POST mapping URL and in the response, we will check for HttpStatus and that the location header contains the URL of the created resource.
MockMvcRequestBuilders.post("/students/Student1/courses").accept(MediaType.APPLICATION_JSON):
Create a post request with an accept header for application\json
content(exampleCourseJson).contentType(MediaType.APPLICATION_JSON):
Use the specified content as
assertEquals(HttpStatus.CREATED.value(), response.getStatus()):
Assert that the return status is CREATED.
response.getHeader(HttpHeaders.LOCATION):
Get the location from response header and later assert that it contains the URI of the created resource.
Suppose we have a following POST Rest Service:
A successful resource creation by POST Service should return a status 201.
and we have the following service for Unit testing in which we have:
@PostMapping("/students/{studentId}/courses"):
A Mapping URL for the POST Request.
@RequestBody Course
A Binding to bind the body of the request to Course object.
Executing a POST Rest Service
Example Request is shown below. It contains all the details to register a course to a student.
{
"name": "Microservices",
"description": "10 Steps",
"steps": [
"Learn How to Break Things Up",
"Automate the hell out of everything",
"Have fun"
]
}
The following example will show you how to apply Unit Test on above POST service:
@Test
public void createStudentCourse() throws Exception {
Course mockCourse = new Course("1", "Smallest Number", "1",Arrays.asList("1", "2", "3", "4"));
// studentService.addCourse to respond back with mockCourse
Mockito.when(studentService.addCourse(Mockito.anyString(),Mockito.any(Course.class))).thenReturn(mockCourse);
// Send course as body to /students/Student1/courses
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/students/Student1/courses")
.accept(MediaType.APPLICATION_JSON)
.content(exampleCourseJson)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.CREATED.value(), response.getStatus());
assertEquals("http://localhost/students/Student1/courses/1",response.getHeader(HttpHeaders.LOCATION));
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kaushlendra Kumar Pal
Kaushlendra is an experienced Java Developer with comprehensive mastery of all Java packages. Able to Write well designed, testable, efficient code as per the requirements and handle different types of issues as well as functions.