Struts2单元测试-1
1、 网络上说对web的action测试没有必要,我想可能action中的功能跟service层的功能粒度差不多,我们可以对service层测试即可。对于Dao层,由于粒度较小,一个业务可能需要多个dao方法,所以可以根据本身项目的测试粒度要求以及项目时间紧张度来做决定是否所有的层都测过去。
2、 Action一般测试通过web客户端点击来进行,如果需要使用代码进行模拟测试,则需要使用以下方法。
3、 struts2中的action测试,由于没有http请求,request和session是为空的,所以我们必须能够模拟web容器的请求行为。在这里Struts2给出了一个插件struts2-junit-plugin-2.1.8.1.jar,这个插件需要spring-test.jar包的支持,这样就可以轻松地进行测试了。
4、 struts2-junit-plugin-2.1.8.1.jar下载地址为:
http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.struts/struts2-junit-plugin/2.1.8
5、 首先测试类必须继承StrutsSpringTestCase这个类,StrutsSpringTestCase是StrutsTestCase的子类,这个单元测试类默认读取配置文件applicationContext.xml(跟spring默认读取一样,放在类路径的根目录下),若此文件在其他位置,我们可以通过重写getContextLocations()来指定你的配置文件。
6、 具体例子:
public class UserActionTest extends StrutsSpringTestCase {
@Autowired
@Before
public void setUp() {
try
{ super.setUp(); }
catch (Exception e)
{ e.printStackTrace(); }
}
@After
public void tearDown() {}
/*
* request是类StrutsSpringTestCase的成员变量,是MockHttpServletRequest对象
* 在这里mock出来的一个web中的一个request
* 通过,request.setParameter("userId", "1");来模拟web传入的参数
* 通过executeAction("/user/*Action!*.action");来模拟调用web链接
*/
@Test public void testRegisterUser() throws UnsupportedEncodingException, ServletException {
request.setParameter("username", "gsdhaiji_cai");
String result = executeAction("/user/user!registerUser.action");
assertEquals("\"{}\"", result);}
}
}
分享到:
相关推荐
struts2结合junit
可解决java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.get(Ljava/lang/Object;... java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.TextUtils struts2.1.8错误
struts2-junit-plugin-2.1.8-sources.jar
struts2-junit-plugin-2.1.8.1.jar
struts2-junit-plugin-2.2.1.1.jar
struts2-junit-plugin-2.3.16.3.jar Java开发常用包
struts2-junit-plugin-2.2.3.1-sources.jar
struts2-junit-plugin-2.1.6.jar struts2测试 action测试 strutstestcase
解决Struct相关包与json相关包不匹配的三个包 导入既可解决struct2.1.8与jsonplugin不匹配的问题 注意:导入前请先将原来的jsonplugin包删除
包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...
struts2-junit-plugin-2.2.3.1.jar
struts2-struts1-plugin-2.3.30.jar, struts2-testng-plugin-2.3.30.jar, struts2-tiles-plugin-2.3.30.jar, struts2-tiles3-plugin-2.3.30.jar, tiles-api-2.2.2.jar, tiles-compat-3.0.1.jar, tiles-core-2.2.2....
Struts2 Spring 框架整合 单元测试 Action和Service http://blog.csdn.net/fansunion/
struts2-junit-plugin-2.5.2.jar, struts2-osgi-admin-bundle-2.5.2.jar, struts2-osgi-demo-bundle-2.5.2.jar, struts2-osgi-plugin-2.5.2.jar, struts2-oval-plugin-2.5.2.jar, struts2-pell-multipart-plugin-...
struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: ...struts2-junit-plugin avadoc.jar
jxl.jar;dom4j.jar;jsonplugin-0.34.jar;junit-3.8.2.jar;log4j-1.2.14.jar;struts2-codebehind-plugin-2.0
gxp-plugin-2.5.10.jar,struts2-jasperreports-plugin-2.5.10.jar,struts2-javatemplates-plugin-2.5.10.jar,struts2-jfreechart-plugin-2.5.10.jar,struts2-json-plugin-2.5.10.jar,struts2-junit-plugin-2.5.10....
struts2-spring-plugin-2.1.6.jar //1 xwork-2.1.2.jar //2 xerces-2.6.2.jar //3 xml-apis.jar //3 注:以下包为数据源连接池 相关 用到那个添加相应的包 c3p0-0.9.1.2.jar //3 commons-pool.jar //3 commons-...