Struts2+Spring+Junit单元测试

使用Junit对Struts+spring系统进行单元测试,好处是不用启动tomcat等容器。

简单实例代码如下:

import org.apache.struts2.StrutsSpringTestCase;

public class ActionTest extends StrutsSpringTestCase{

	@Override
	protected String getContextLocations() {
		return "classpath*:applicationContext*.xml";
	}

	public void testAll() throws Exception{
		request.addParameter("id", "1");
		String res=executeAction("/test.action");
		System.out.println(res);
	}
}


主要是继承StrutsSpringTestCase类。

测试用例中,action返回的数据为json,res即为response的内容。

你可能感兴趣的:(spring,struts,JUnit)