- Spring Boot & H2 - Discussion
- Spring Boot & H2 - Useful Resources
- Spring Boot & H2 - Quick Guide
- Spring Boot & H2 - Unit Test Repository
- Spring Boot & H2 - Unit Test Service
- Spring Boot & H2 - Unit Test Controller
- Spring Boot & H2 - Delete Record
- Spring Boot & H2 - Update Record
- Spring Boot & H2 - Get All Records
- Spring Boot & H2 - Get Record
- Spring Boot & H2 - Add Record
- Spring Boot & H2 - H2 Console
- Spring Boot & H2 - REST APIs
- Spring Boot & H2 - Project Setup
- Spring Boot & H2 - Environment Setup
- Spring Boot & H2 - Overview
- Spring Boot & H2 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring Boot & H2 - Unit Test Controller
As in previous chapter we ve completed our REST APIs. Now let s create the following structure in src/main/test folder.
com.tutorialspoint.controller.EmployeeControllerTest − A Unit Tester Class to unit test all methods of EmployeeController.
com.tutorialspoint.repository.EmployeeRepositoryTest − A Unit Tester Class to unit test all methods of EmployeeRepository.
com.tutorialspoint.service.EmployeeServiceTest − A Unit Tester Class to unit test all methods of EmployeeService.
SprintBootH2ApppcationTests class is already present. We need to create the above packages and relevant classes.
EmployeeControllerTest
To test a REST Controller, we need the following annotation and classes −
@ExtendWith(SpringExtension.class) − Mark the class to run as test case using SpringExtension class.
@SpringBootTest(classes = SprintBootH2Apppcation.class) − Configure the Spring Boot apppcation.
@AutoConfigureMockMvc − To automatically configure the MockMVC to mock HTTP Requests and Response.
@Autowired private MockMvc mvc; − MockMvc object to be used in testing.
@MockBean private EmployeeController employeeController − EmployeeController mock object to be tested.
Following is the complete code of EmployeeControllerTest.
package com.tutorialspoint.controller; import static org.hamcrest.core.Is.is; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doNothing; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import com.fasterxml.jackson.databind.ObjectMapper; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.sprintbooth2.SprintBootH2Apppcation; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = SprintBootH2Apppcation.class) @AutoConfigureMockMvc pubpc class EmployeeControllerTest { @Autowired private MockMvc mvc; @MockBean private EmployeeController employeeController; @Test pubpc void testGetAllEmployees() throws Exception { Employee employee = getEmployee(); List<Employee> employees = new ArrayList<>(); employees.add(employee); given(employeeController.getAllEmployees()).willReturn(employees); mvc.perform(get("/emp/employees/").contentType(APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$[0].name", is(employee.getName()))); } @Test pubpc void testGetEmployee() throws Exception { Employee employee = getEmployee(); given(employeeController.getEmployee(1)).willReturn(employee); mvc.perform(get("/emp/employee/" + employee.getId()).contentType(APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("name", is(employee.getName()))); } @Test pubpc void testDeleteEmployee() throws Exception { Employee employee = getEmployee(); doNothing().when(employeeController).deleteEmployee(1); mvc.perform(delete("/emp/employee/" + employee.getId()).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); } @Test pubpc void testAddEmployee() throws Exception { Employee employee = getEmployee(); doNothing().when(employeeController).addEmployee(employee); mvc.perform(post("/emp/employee/").content(asJson(employee)).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); } @Test pubpc void testUpdateEmployee() throws Exception { Employee employee = getEmployee(); doNothing().when(employeeController).updateEmployee(employee); mvc.perform(put("/emp/employee/").content(asJson(employee)).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); } private Employee getEmployee() { Employee employee = new Employee(); employee.setId(1); employee.setName("Mahesh"); employee.setAge(30); employee.setEmail("mahesh@test.com"); return employee; } private static String asJson(final Object obj) { try { return new ObjectMapper().writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException(e); } } }
Run the test cases.
Right Cpck on the file in ecppse and select Run a JUnit Test and verify the result.
Advertisements