'MockServletContext'에 해당되는 글 1건
- 2013.05.20 :: Spring 3.1 TestCase (MockServletContext 사용)
svn 다운로드 주소 : https://faz.googlecode.com/svn/trunk/springtestcase3.1
좀 틀린부분도 있습니다. 너그러이 용서를....
package kr.faz.testcase;
import javax.inject.Inject;
import kr.faz.util.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:kr/faz/test-servlet-context.xml" )
@Transactional
public class MockControllerTest {
@Inject
private RequestMappingHandlerAdapter handlerAdapter;
@Inject
private RequestMappingHandlerMapping handlerMapping;
MockHttpServletRequest request ;
MockHttpServletResponse response;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
@Test //주문서로 이관
public void urlTest() throws Exception {
setRequest("/test.do?companycode=2&infolidx=2&filelidx=2&orderdate=20130413", "POST");
}
public void setRequest(String uri, String method) throws Exception {
int questionIndex = uri.indexOf("?");
String url = null;
String param = null;
if ( questionIndex != -1 ){
url = uri.substring(0, questionIndex);
param = uri.substring(questionIndex+1);
} else {
url = uri;
}
request.setRequestURI(url);
if ( StringUtils.isNotEmpty(param)) {
String params[] = param.split("&");
for ( String value : params) {
//파라미터 값이 있으면 셋팅
String[] v = value.split("=");
if ( v.length == 2) {
request.addParameter(v[0], v[1]);
}
}
}
System.out.println("URI : "+ request.getRequestURI());
System.out.println("URL : "+ request.getRequestURL());
System.out.println("QueryString : "+ request.getQueryString());
request.setMethod(method);
Object handler = handlerMapping.getHandler(request).getHandler();
handlerAdapter.handle(request, response, handler);
System.out.println("response data : "+ response.getContentAsString());
request.close();
}
}