2012-10-05 125 views
1

我试图在Spring MVC中测试Handler映射的存在。这将有助于我抽象一些自定义情况,其中某个请求需要由非标准处理程序映射来处理。Spring MVC处理程序映射测试

我真的没有看到一个简单的方法来说:映射“/ * /注册/注册/自定义”,它存在吗?

任何想法?

马克

回答

0

简单的方法来测试映射:

import java.net.HttpURLConnection; 
import java.net.URL; 
import junit.framework.TestCase; 
import org.junit.Test; 
public class HomeControllerTest extends TestCase{ 

    @Test 
    public void test() { 
     assertEquals(true, checkIfURLExists("http://localhost:8080/test")); 
    } 


    public static boolean checkIfURLExists(String targetUrl) { 
     HttpURLConnection httpUrlConn; 
     try { 
      httpUrlConn = (HttpURLConnection) new URL(targetUrl).openConnection(); 
      httpUrlConn.setRequestMethod("GET"); 

      // Set timeouts in milliseconds 
      httpUrlConn.setConnectTimeout(30000); 
      httpUrlConn.setReadTimeout(30000); 

      // Print HTTP status code/message for your information. 
      System.out.println("Response Code: " + httpUrlConn.getResponseCode()); 
      System.out.println("Response Message: " + httpUrlConn.getResponseMessage()); 

      return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK); 
     } catch (Exception e) { 
      System.out.println("Error: " + e.getMessage()); 
      return false; 
     } 
    } 
} 

从春天文档: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/#unit-testing-spring-mvc

9.2.2.2 Spring MVC的

的org.springframework.test。 web包包含ModelAndViewAssert, wh ich可以结合使用JUnit 4+,TestNG等,以便处理Spring MVC ModelAndView对象的单元测试 。

单元测试Spring MVC的控制器来测试你的Spring MVC 控制器,使用ModelAndViewAssert与 MockHttpServletRequest,MockHttpSession,等从 org.springframework.mock.web包相结合。

+0

这似乎并没有提供所需的功能。 – Marc