如何为RestController、Service和DAO 层编写JUnit测试用例?
我试过了MockMvc
@RunWith(SpringRunner.class)
public class EmployeeControllerTest {
private MockMvc mockMvc;
private static List<Employee> employeeList;
@InjectMocks
EmployeeController employeeController;
@Mock
EmployeeRepository employeeRepository;
@Test
public void testGetAllEmployees() throws Exception {
Mockito.when(employeeRepository.findAll()).thenReturn(employeeList);
assertNotNull(employeeController.getAllEmployees());
mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/employees"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
如何验证其余控制器和其他层内的CRUD方法?
炎炎设计
相关分类