2013-01-14 33 views
0

我使用彈簧3的Java小服務程序。 有什麼辦法來檢查是否有特定的URL處理程序?春季3檢查是否有一個URL的處理程序

我試圖執行一個測試,確保所有在我的Jsp文件中使用的URL都被處理。 如果我想做一個URL重構,我希望確保沒有任何「斷鏈」在我的JSP ...

感謝

回答

1

這裏FooController的,如果測試的例子您使用JUnit和春季3:

@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()); 
    } 
} 
+0

謝謝:)。你可以添加一些評論(或解釋)?..因爲我不知道我是否知道它是如何工作的。我在春天是相對新的 – ApollonDigital

+0

評論更新 –

+0

非常感謝;) – ApollonDigital