0

I have the below Velocity config for both test and main in my spring mvc app:

@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;
}   

It works correctly when I deploy the WAR and all my MockMvcResultMatchers in test except for `forwardedUrl which returns 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()))));

The appropriate Controller under test has the following beginning:

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

The stacktrace begins:

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

Why is the forwarded URL incorrect? Is it an issue with the MockMvc or the velocity setup?

user1561108
  • 2,666
  • 9
  • 44
  • 69
  • where are mapping `/` to `layout.vm`? – Braj Mar 18 '16 at 07:56
  • default spring mvc behaviour I believe - see a comment on an earlier question - http://stackoverflow.com/questions/35936650/error-org-apache-velocity-resourcemanager-unable-to-find-resource-layout-vm#comment59542084_35937701 – user1561108 Mar 18 '16 at 07:58
  • Here is same [question](http://stackoverflow.com/questions/20782970/mockmvc-forwardedurl-is-null) – Braj Mar 18 '16 at 07:59
  • that is not the same issue as mine – user1561108 Mar 18 '16 at 08:00

0 Answers0