我在spring 3中使用java servlet。 有没有办法检查是否有特定URL的处理程序?
我正在尝试实施一项测试,以确保处理我的Jsp文件中使用的所有网址。 如果我想进行网址重构,我想确保没有任何“破解链接”。在我的jsps ...
由于
答案 0 :(得分:1)
如果你正在使用JUnit和Spring 3,这里是一个FooController测试的例子:
@Controller
@RequestMapping(value = "/foo")
public class FooAdminController {
@RequestMapping(value = "/bar")
public ModelAndView bar(ModelAndView mav) {
mav.setViewName("bar");
return mav;
}
}
FooController的测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/path/to/servlet-context.xml" })
public class FooControllerTest {
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@Autowired
private RequestMappingHandlerAdapter handleAdapter;
@Test
public void fooControllerTest() throws Exception{
// Create a Mock implementation of the HttpServletRequest interface
MockHttpServletRequest request = new MockHttpServletRequest();
// Create Mock implementation of the HttpServletResponse interface
MockHttpServletResponse response = new MockHttpServletResponse();
// Define the request URI needed to test a method on the FooController
request.setRequestURI("/foo/bar");
// Define the HTTP Method
request.setMethod("GET");
// Get the handler and handle the request
Object handler = handlerMapping.getHandler(request).getHandler();
ModelAndView handleResp = handleAdapter.handle(request, response, handler);
// Test some ModelAndView properties
ModelAndViewAssert.assertViewName(handleResp ,"bar");
assertEquals(200, response.getStatus());
}
}