Wednesday, July 11, 2012

Spring MVC Integration Tests - Correction

In a previous blog entry - Spring MVC Integration Tests I had mentioned that one of the issues in using Spring-test-mvc is that any resource required from the WEB-INF folder is not available in the unit tests. This is actually not correct.

Resources under WEB-INF location can also be used with spring-test-mvc, this way:

xmlConfigSetup("/WEB-INF/spring/webmvc-config.xml","classpath:/org/bk/lmt/web/contextcontrollertest.xml")
 .configureWebAppRootDir("src/main/webapp", false).build()
 .perform(post("/contexts").param("name", "context1"))
 .andExpect(status().isOk())
 .andExpect(view().name("redirect:/contexts"));

xmlConfigSetup("/WEB-INF/spring/webmvc-config.xml", "classpath:/org/bk/lmt/web/contextcontrollertest.xml")
 .configureWebAppRootDir("src/main/webapp", false).build()
 .perform(get("/contexts"))
 .andExpect(status().isOk())
 .andExpect(view().name("contexts/list"));

the Web Application path can be specified as shown in the highlighted lines above. This works because the unit test current working directory is typically the root of the project, and if the structure is a typical maven project structure then the web root will be "src/main/webapp" folder which can be resolved from the root of the project.

No comments:

Post a Comment