POST
@Test void addEmployeeDetails() throws Exception { Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json"); String jsonRequestString = mapper.writeValueAsString(objEmployee); when(employeeServiceImpl.saveEmployee(any())).thenReturn("101"); ResultActions objResultActions = this.mockMvc.perform(post("/empmgmt/employees") .contentType(MediaType.APPLICATION_JSON) .content(jsonRequestString)) .andDo(print()) .andExpect(jsonPath("$.empID", is(Matchers.notNullValue()))) .andExpect(jsonPath("$.empID", is(101))) .andExpect(jsonPath("$.empName", is("Mani"))) .andExpect(status().isCreated()); }
GET
@Test void getEmployeeDetails() throws Exception { ClassLoader loader = Test.class.getClassLoader(); Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json"); Optional<Employee> objEmp = Optional.ofNullable(objEmployee); when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp); ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees/{empID}",EMP_ID)) .andDo(print()) .andExpect(jsonPath("$.empID", is(101))) .andExpect(jsonPath("$.empName", is("Mani"))) .andExpect(jsonPath("$._links.all-employees.href", is("http://localhost/empmgmt/employees"))) .andExpect(status().isOk()); }
DELETE
@Test void deleteEmployeeDetails() throws Exception { when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true); this.mockMvc.perform(delete("/empmgmt/employees/{empID}",EMP_ID)) .andDo(print()) .andExpect(content().string(containsString("Employee Deleted Successfully"))) .andExpect(status().isOk()); }
EmployeeControllerTest.java
package com.mugil.org.controllers; import com.fasterxml.jackson.databind.ObjectMapper; import com.mugil.org.TestTraits; import com.mugil.org.models.Employee; import com.mugil.org.services.EmployeeServiceImpl; import org.hamcrest.Matchers; 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.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import java.util.ArrayList; import java.util.List; import java.util.Optional; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ExtendWith(SpringExtension.class) @WebMvcTest(EmployeeController.class) class EmployeeControllerTest { @MockBean EmployeeServiceImpl employeeServiceImpl; ObjectMapper mapper = new ObjectMapper(); @Autowired private MockMvc mockMvc; private static final String EMP_ID = "101"; @Test void getEmployeeDetails() throws Exception { ClassLoader loader = Test.class.getClassLoader(); Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json"); Optional<Employee> objEmp = Optional.ofNullable(objEmployee); when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp); ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees/{empID}",EMP_ID)) .andDo(print()) .andExpect(jsonPath("$.empID", is(101))) .andExpect(jsonPath("$.empName", is("Mani"))) .andExpect(jsonPath("$._links.all-employees.href", is("http://localhost/empmgmt/employees"))) .andExpect(status().isOk()); } @Test void deleteEmployeeDetails() throws Exception { when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true); this.mockMvc.perform(delete("/empmgmt/employees/{empID}",EMP_ID)) .andDo(print()) .andExpect(content().string(containsString("Employee Deleted Successfully"))) .andExpect(status().isOk()); } @Test void getAllEmployeeDetails() throws Exception { List<Employee> arrEmployees = new ArrayList<>(); arrEmployees.add(getEmployee()); when(employeeServiceImpl.findAllEmployee()).thenReturn(arrEmployees); ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees") .contentType(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(jsonPath("$[0].empID", is(Matchers.notNullValue()))) .andExpect(jsonPath("$[0].empID", is(101))) .andExpect(jsonPath("$[0].empName", is("Shivaji"))) .andExpect(status().is2xxSuccessful()); } @Test void addEmployeeDetails() throws Exception { Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json"); String jsonRequestString = mapper.writeValueAsString(objEmployee); when(employeeServiceImpl.saveEmployee(any())).thenReturn("101"); ResultActions objResultActions = this.mockMvc.perform(post("/empmgmt/employees") .contentType(MediaType.APPLICATION_JSON) .content(jsonRequestString)) .andDo(print()) .andExpect(jsonPath("$.empID", is(Matchers.notNullValue()))) .andExpect(jsonPath("$.empID", is(101))) .andExpect(jsonPath("$.empName", is("Mani"))) .andExpect(status().isCreated()); } private Employee getEmployee(){ Employee objEmployee = new Employee(); objEmployee.setEmpID(Long.valueOf(101)); objEmployee.setEmpAge("35"); objEmployee.setEmpName("Shivaji"); return objEmployee; } }