![](/img/trans.png)
[英]Spring Security causing problems when doing unit tests on REST controller
[英]Problems with Unit_Tests
我正在尝试在 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?
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())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.