我正在嘗試在 MVC-Spring Boot 中為我的控制器撰寫 UNIT 測驗,實際上我是新手。我在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?
uj5u.com熱心網友回復:
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://www.uj5u.com/qiye/491600.html
