Spring MVC Controller测试因未找到模型属性而传递(Spring MVC Controller Test passing due to not finding model attri

编程入门 行业动态 更新时间:2024-10-23 17:39:33
Spring MVC Controller测试因未找到模型属性而传递(Spring MVC Controller Test passing due to not finding model attribute)

我在下面创建了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)

更多推荐

本文发布于:2023-07-04 11:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未找到   属性   模型   测试   Controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!