POST
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | @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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | @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(status().isOk()); } |
DELETE
1 2 3 4 5 6 7 8 9 | @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
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 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(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; } } |