[英]@ModelAttribute in Spring Boot application
I have a Spring Boot application (embedded Tomcat, Thymeleaf template...) Here one of my controllers: 我有一个Spring Boot应用程序(嵌入式Tomcat,Thymeleaf模板...),这是我的控制器之一:
@Controller
@RequestMapping("/company")
public class CompanyController {
@RequestMapping(value = { "/list" }, method = { RequestMethod.GET })
public String companyList(Company company, ModelMap model) {
return serverContextPath + COMPANY_LIST_VIEW_NAME;
}
@ModelAttribute("companies")
public Iterable<Company> companies(){
return companyService.findAll();
}
}
I've created this Junit Test to verify that my controller works fine 我创建了这个Junit测试来验证我的控制器工作正常
@ContextConfiguration(classes = { MyApplicationConfig.class })
public class CompanyControllerTests {
private CompanyController controller;
@Value("${server.contextPath}")
protected String serverContextPath;
@Before
public void setUp() throws Exception {
controller = new CompanyController();
}
@Test
public void testCompanyList() {
ExtendedModelMap model = new ExtendedModelMap();
String viewName = controller.companyList(new Company(), model);
assertEquals(serverContextPath + CompanyController.COMPANY_LIST_VIEW_NAME, viewName);
Iterable<Company> companies = (Iterable<Company>) model.get("companies");
assertNotNull(companies);
}
}
But I have an AssertionError assertNotNull(companies);
但是我有一个AssertionError
assertNotNull(companies);
Probably you need to mock this call with some expected result: 可能您需要模拟此调用并获得一些预期结果:
companyService.findAll()
to be able to test controller apart from services 能够测试除服务之外的控制器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.