Struts2 + Spring + iBatis 환경에서 MVC 테스트 방법을 찾아봤다.

POJO(s/getter) 방식과 Request s/getAttribute 방식 두가지로 결과값을 받아 데이터를 검증해보자.


[GoodsActionCase.java]

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;

import com.opensymphony.xwork2.ActionProxy;;

public class GoodsActionCase extends StrutsTestCaseSupport {

	@Test
	public void 상품상세() throws Exception {
		Map sessionMap = new HashMap();

		// /view/goodsDetail.action
		ActionProxy proxy = StrutsTestCaseSupport.getInstance().createActionProxy ("detail", "/goods", sessionMap);
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.setParameter("코드", "123456");
		ServletActionContext.setRequest(request);


		GoodsAction goodsAction = (GoodsAction) proxy.getAction();
		goodsAction.detail();

		GoodsInfoBean memberFieldData 	= goodsAction.getDataBean();
		GoodsInfoBean setAttributeData 	= (GoodsInfoBean) ServletActionContext.getRequest().getAttribute("goodsInfo");

		assertThat("널이 아니겠지?", memberFieldData, is(notNullValue()));
		assertThat("널이 아니겠지?", setAttributeData, is(notNullValue()));
		assertThat("앞뒤가 같겠지?", memberFieldData.getGoodsNm(), equalTo(setAttributeData.getGoodsNm()));
	}
}

[환경]

Struts2 2.0.x

Spring 2.5.x

StrutsTestCaseSupport.java - http://fassisrosa.blogspot.kr/2007/09/unit-testing-struts-20-part-3.html


+ Recent posts