Spring MVC 单元测试Demo
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC 单元测试Demo相关的知识,希望对你有一定的参考价值。
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:webApplicationContext.xml"}) public class XXXXControllerTest{ private Logger logger = LoggerFactory.getLogger(XXXXServiceTest.class); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void startUp(){ mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void mock_mainPageInfo_index() throws Exception { String requestUrl = "/index?userId={userId}"; String[] midValue = new String[]{"369350"}; MvcResult result = mockMvc.perform(get(requestUrl,midValue)).andReturn(); ModelAndView modelAndView = result.getModelAndView(); ModelMap modelMap = modelAndView.getModelMap(); Assert.assertEquals("/mainPageInfo/index", modelAndView.getViewName()); } }
以上是关于Spring MVC 单元测试Demo的主要内容,如果未能解决你的问题,请参考以下文章
spring mvc+spring + hibernate 整合
使用 Spring-Test-MVC 单元测试 Spring-Security - 集成 FilterChain / ServletFilter