0

我正在使用带有 spring 3 的 java servlet。有没有办法检查是否有特定 URL 的处理程序?

我正在尝试实现一个测试,以确保处理我的 Jsp 文件中使用的所有 url。如果我想进行 url 重构,我想确保我的 jsps 中没有任何“断开的链接”......

谢谢

4

1 回答 1

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());
    }
}
于 2013-01-15T00:04:19.943 回答