0

我在我的 spring mvc 应用程序中为 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的所有测试时,它可以正常工作,除了返回 null 的 `forwardedUrl:

@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...

为什么转发的 URL 不正确?是 MockMvc 还是速度设置的问题?

4

0 回答 0