我在下面创建了Home Controller。 该控制器通过PostService类获取我在“PostRepository”类中创建的5个虚拟帖子。
@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")); } }测试成功通过。 但属性应该存在。
I have created the Home Controller below. This controller fetches the 5 dummy posts I have created in the "PostRepository" class through PostService class.
@Controller public class HomeController { @Autowired PostService postService; @RequestMapping("/") public String getHome(Model model){ model.addAttribute("Post", postService); return "home"; } }I have implemented the following test..
@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")); } }The test has successfully passed. But the attribute should exist.
最满意答案
您正在混合Spring的测试支持的两个不兼容的功能。
如果在测试中实例化控制器,则需要使用MockMvcBuilders.standaloneSetup() 。
如果您正在使用Spring TestContext Framework (即@ContextConfiguration等),那么您需要使用MockMvcBuilders.webAppContextSetup() 。
因此,以下是您的测试的适当配置。
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = WebConfig.class) @WebAppConfiguration public class ControllerTest { @Autowired WebApplicationContext wac; @Autowired PostService postService; @Test public void TestHomePage2() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); mockMvc.perform(get("/")) .andExpect(view().name("home")) .andExpect(model().attribute("Post",postService)); } }问候,
Sam( Spring TestContext Framework的作者 )
You are mixing two incompatible features of Spring's testing support.
If you instantiate the controller within the test, you need to use MockMvcBuilders.standaloneSetup().
If you are using the Spring TestContext Framework (i.e., @ContextConfiguration, etc.), then you need to use MockMvcBuilders.webAppContextSetup().
Thus, the following is the appropriate configuration for your test.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = WebConfig.class) @WebAppConfiguration public class ControllerTest { @Autowired WebApplicationContext wac; @Autowired PostService postService; @Test public void TestHomePage2() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); mockMvc.perform(get("/")) .andExpect(view().name("home")) .andExpect(model().attribute("Post",postService)); } }Regards,
Sam (author of the Spring TestContext Framework)
更多推荐
发布评论