我有一个测试:
@Test
public void shouldAddCompany() throws Exception {
mockMvc.perform(post("/companies")
.param("name","companyName"))
.andExpect(model().attribute("company",hasProperty("name",is("companyName"))));
}
我的控制器方法看起来像这样:
@PostMapping("/companies")
public String displayCompaniesPost(@modelattribute Company company) {
companyService.save(company);
return "redirect:/companies";
}
java.lang.AssertionError: Model attribute 'company'
Expected: hasProperty("name",is "companyName")
but: was null
我认为这是因为控制器因重定向而转向GET方法.当我删除此重定向时一切正常,但我不想删除该重定向.
编辑(GetMapping):
@GetMapping({"/","/companies"})
public String displayCompanies(Model model) {
model.addAttribute("company",new Company());
List
最佳答案
原文链接:https://www.f2er.com/spring/432052.html