MockMvc:预期转发的URL:但是:<null>

时间:2016-03-18 07:52:37

标签: unit-testing spring-mvc velocity matcher mockmvc

我的spring mvc app中有test和main的以下Velocity配置:

@Bean
public VelocityConfigurer velocityConfig(){
    VelocityConfigurer velocityConfig = new VelocityConfigurer();
    velocityConfig.setResourceLoaderPath("/WEB-INF/views/");
    return velocityConfig;
}

@Bean
public VelocityLayoutViewResolver viewResolver(){
    VelocityLayoutViewResolver viewResolver = new VelocityLayoutViewResolver();
    viewResolver.setCache(true);
    viewResolver.setLayoutUrl("layout.vm");
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".vm");
    return viewResolver;
}   

当我在测试中部署WAR和我的MockMvcResultMatchers时,它正常工作,除了`forwardedUrl返回null:

@Test
public void testHome() throws Exception{
    mockMvc.perform(MockMvcRequestBuilders.get("/"))
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.view().name("index"))
        .andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/layout.vm"))
        .andExpect(MockMvcResultMatchers.model().attribute("myData", Matchers.is(Matchers.not(Matchers.empty()))));

正在测试的相应控制器具有以下开头:

@RequestMapping({"/index", "/"})
public ModelAndView home(){

堆栈跟踪开始:

java.lang.AssertionError: Forwarded URL expected:</WEB-INF/views/layout.vm> but was:<null>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
    at org.springframework.test.web.servlet.result.MockMvcResultMatchers$1.match(MockMvcResultMatchers.java:93)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)
    at com...

为什么转发的网址不正确?这是MockMvc或速度设置的问题吗?

0 个答案:

没有答案