我正在使用 Spring 3.1.1.RELEASE 和 JUnit 4.11。我像这样设置我的 JUnit 测试
@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;
在测试控制器时,我有这行来验证控制器的方法返回的视图是正确的视图……</p>
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);
}
我的问题是,一旦我验证了控制器返回的视图是预期的视图,我如何验证它不会导致 404?我现在给出的主要问题是测试视图是否实际映射到我的 WAR 文件中的底层页面。