测试Spring MVC处理程序映射

时间:2012-10-05 17:53:54

标签: spring spring-mvc

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

我真的没有看到简单的说法:映射" / * / registration / register / custom",它是否存在?

有什么想法吗?

马克

1 个答案:

答案 0 :(得分: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;
        }
    }
}

来自Spring文档: 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,   您可以将它与JUnit 4 +,TestNG等结合使用   处理Spring MVC ModelAndView对象的单元测试。

     

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

相关问题