Pregunta

I'm using Spring 3.1.1.RELEASE and JUnit 4.11. I setup my JUnit tests like so

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class MySpringTest 
{
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;
    protected MockHttpSession session;

    @Autowired
    protected RequestMappingHandlerAdapter handlerAdapter;

    @Autowired
    protected RequestMappingHandlerMapping handlerMapping;

When testing controllers, I have this line to verify that the view the controller's method is returning is the right view …

import static org.springframework.test.web.ModelAndViewAssert.assertViewName;
...
final ModelAndView mav = submitMyForm(…);
    assertViewName(mav, "folder/myView");
    ...

protected ModelAndView submitMyForm(… params ...) throws Exception {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setRequestURI("/myurl");
    request.setMethod("POST");
    request.addParameter("param1", param1);
    ...

    final Object handler = handlerMapping.getHandler(request).getHandler();
    return handlerAdapter.handle(request, response, handler);
} 

My question is, once I verify the view returned my the controller is the expected view, how do I verify it won't result in a 404? The main problem I'm gaving now is testing whether or not the view actually maps to an underlying page in my WAR file.

¿Fue útil?

Solución

why don't use spring-mvc-test and do something like this ?

@Autowired
private ViewResolver viewResolver;

// code

View view = viewResolver.resolveViewName(viewName, locale);

//assert view not null

or something like this, in wich you can check both if the view is ok and the returned status (is status 200/404?) (more code here: http://goo.gl/fMqBsl)

@Test
public void indexTest() throws Exception {

    mockMvc.perform(get("/")).andDo(print())
        .andExpect(handler().handlerType(MainController.class))
        .andExpect(handler().methodName("index"))
        .andExpect(view().name("index"))
        .andExpect(forwardedUrl("/WEB-INF/tiles/template.jsp"))
        .andExpect(status().isOk());
}

Otros consejos

i am using standard jsp view

basically, you need to know the view resolver(s). can a specific view be resolved? that means, if you DON'T have a file called abc.xml, it might still be a valid view.

for simplicity sake, lets assume that we have only one view resolver, and, its

"org.springframework.web.servlet.view.UrlBasedViewResolver"

and here is the bean definition spring 3.2.4 documentation pdf, page 477

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

eg: the view name "page1" => /WEB-INF/jsp/page1.jsp and "admin/page2" => /WEB-INF/jsp/admin/page2.jsp

using this, you can Inject the view resolved to your junit test using @Autowired and/or @Qualifier then read the "prefix" and suffix value and find the full path like "src/main/webapp/" + prefix + viewname + suffix

and check if the file exists.

you may have multiple view resolvers, so you may want to inject the context and handle the view => filename resolution using a strategy pattern.

something like

foreach resolver
{
if i can resolve the view to a file (resolver type, viewname)
return the physical filename
else
try next resolver
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top