0
Ich habe einen Frühling Boot-Anwendung (embedded Tomcat, Thymeleaf Vorlage ...) Hier eine meiner Controller:@ModelAttribute im Frühjahr Boot-Anwendung
@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();
}
}
ich diesen JUnit-Test erstellt haben, um sicherzustellen, dass mein Controller funktioniert
@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);
}
}
aber ich habe eine AssertionError assertNotNull(companies);
Und warum das gefüllt werden soll? Sie rufen eine Methode direkt auf, Sie verwenden die Spring MVC-Infrastruktur nicht zum Testen. –
MockMvc ist, was Sie brauchen, um einen Controller zu testen. – akuma8