我在下面创建了主控制器。此控制器通过PostService类获取我在“PostRepository”类中创建的5个虚拟帖子。Spring MVC Controller由于未找到模型属性而导致测试通过
@Controller
public class HomeController {
@Autowired
PostService postService;
@RequestMapping("/")
public String getHome(Model model){
model.addAttribute("Post", postService);
return "home";
}
}
我已经实现了以下测试..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebConfig.class})
@WebAppConfiguration
public class ControllerTest {
@Test //Test the Home Controller
public void TestHomePage() throws Exception{
HomeController homeController = new HomeController();
MockMvc mockMvc = standaloneSetup(homeController).build();
mockMvc.perform(get("/"))
.andExpect(view().name("home"))
.andExpect(model().attributeDoesNotExist("Post"));
}
}
测试已顺利通过。但属性应该存在。
(春季TestContext框架作者)你应该把一个更完整的代码(至少你完整的HomeController,整个错误消息,看看哪些行是错误的,...) – RemyG
@RemyG,谢谢!我已根据您的建议更新了该文章。 –