我试图在Spring MVC中测试Handler Mappings的存在。这将有助于我抽象一些自定义案例,其中某个请求需要由非标准处理程序映射处理。
我真的没有看到简单的说法:映射" / * / registration / register / custom",它是否存在?
有什么想法吗?
马克
答案 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;
}
}
}
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包。