我试图用MVC - Spring为我的控制器编写单元测试,实际上我是新手。我在pom/xml中增加了单元测试的依赖性。这是我的控制器:
@GetMapping("/showFormForUpdate/{id}")
public String showFormForUpdate(@PathVariable ( value = "id") long id, Model model) {
// get employee from the service
Employee employee = employeeService.getEmployeeById(id);
// set employee as a model attribute to pre-populate the form
model.addAttribute("employee", employee);
return "update_employee";
}以下是我所做的:
公共类ControllerTests {
@Test
void hello(){
EmployeeController controller = new EmployeeController();//Arrange
String response = controller.showFormForUpdate( long id);
}}
How could i write a good Unit test for this?发布于 2022-06-02 01:31:11
Spring为控制器层切片测试提供了@WebMvcTest。(严格地说,这不是单元测试。但也没有进行综合测试。
https://spring.io/guides/gs/testing-web/
例如
@WebMvcTest
public class YourTest() {
@Autowired
private MockMvc mockMvc;
@Test
public void hello() {
this.mockMvc
.perform(get("/showFormForUpdate/111"))
.andDo(print())
.andExpect(status().isOk())
}
}https://stackoverflow.com/questions/72456585
复制相似问题