现在的struts2 版比 struts1 测试显的更简单了,我们只需引入struts-junit.jar插件和spring的一些架包即可进行测试.下面的这些架包是必需的(以struts2.1.8.1版为例):
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
freemarker-2.3.15.jar
struts2-junit-plugin-2.1.8.1.jar
commons-logging-1.0.4.jar
commons-fileupload-1.2.1.jar
junit-4.8.2.jar
spring-test-2.5.6.jar
spring-core-2.5.6.jar(如果你还想使用Spring的其他功能,直接引入独立包spring.jar就行了)
以上都是必需的,不过特别要注意的是在不用spring当JavaBean工厂的时候,千万别加入struts2-spring-plugin-2.1.8.1.jar 这个架包,要不然你运行测试程序时它死活不让你通过,就在控制台一直提醒你未配置spring监听.
下面写些例子来说明怎么测试struts2:
被测试的Action:
public class MyAction extends ActionSupport implements ServletResponseAware{
private String name;
private HttpServletResponse response;
@Override
public String execute() throws Exception {
response.setContentType("text/html; charset=utf-8");
response.getWriter().write("Hello world!");
response.getWriter().flush();
return SUCCESS;
}
public String hello() throws Exception {
//...
return "hello";
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
//name's getter and setter
}
测试类:
public class MyActionTest extends StrutsTestCase {
//测试Action配置文件是否正确
public void testGetActionMapping() {
ActionMapping mapping = getActionMapping("/test");
assertNotNull(mapping);
assertEquals("/", mapping.getNamespace());
assertEquals("test", mapping.getName());
}
//测试Action代理
public void testGetActionProxy() throws Exception {
//set parameters before calling getActionProxy
request.setParameter("name", "Chase");
ActionProxy proxy = getActionProxy("/test");
assertNotNull(proxy);
MyAction action = (MyAction) proxy.getAction();
assertNotNull(action);
String result = proxy.execute();
assertEquals(ActionSupport.SUCCESS, result);
assertEquals("Chase", action.getName());
}
//测试重写的execute()方法
public void testExecuteAction() throws ServletException, UnsupportedEncodingException {
String output = executeAction("/test");
assertEquals("Hello world!", output);
}
//测试自定义方法
public void testMyMethod() throws Exception {
ActionProxy proxy = getActionProxy("/helloAction");
assertNotNull(proxy);
MyAction action = (MyAction) proxy.getAction();
assertNotNull(action);
String result = proxy.execute();
assertEquals("hello", result);
}
//测试value stack
public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException {
request.setParameter("name", "Chase");
executeAction("/test");
String name = (String) findValueAfterExecute("name");
assertEquals("Chase", name);
}
}
struts.xml部分:
<package name="testPackage" extends="struts-default" namespace="/">
<action name="test" class="com.chase.action.MyAction">
<result>/test-success.jsp</result>
</action>
<action name="*Action" class="com.chase.action.MyAction" method="{1}">
<result name="{1}">/test-success.jsp</result>
</action>
</package>
分享到:
相关推荐
Struts2 Spring 框架整合 单元测试 Action和Service http://blog.csdn.net/fansunion/
Struts+Spring框架下的单元测试
Struts2框架单元测试代码 一个完整的Eclipse工程
Struts2+Spring+hibernate中对action的单元测试环境搭建[总结].pdf
参考博文:http://blog.csdn.net/u011638419/article/details/41407401
mock struts1 测试
单元测试 之 16.3 测试完整的Struts2运行流程
13.2 对动作进行单元测试 281 13.2.1 将IoC用于测试的优势 281 13.2.2 JUnit与测试 282 13.2.3 测试validation.xml文件 284 13.3 最大化重用 284 13.3.1 使用component标签组件化 285 13.3.2 重用模板化的标签 286 ...
Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,...单元测试困难 这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0,它的诞生能很好的解决上述问题。
7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-spring-1.2.xsd(ehcache-spring-annotations...
Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功。 然而,很多初学者在集成这3个框架的时候,总是会遇到各种各样的问题。 我在读大学刚刚学习SSH的...
2、在sping配置中注意单元测试里,xml文件的路径问题,要用绝对路径。 3、struts.xml中命名空间和拦截器的使用。 4、sqlmapconfig.xml中传入参数和result的设置,只需要将得到的值result映射到有此属性的类对象. 5、...
jdk1.4下struts2应用实例,自己写的,整合spring,spring测试框架
Struts+Spring框架下的单元测试[总结].pdf
Struts作为MVC 2的Web框架,自推出以来...单元测试困难 这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0,它的诞生能很好的解决上述问题。 好啦,废话就不多说了,现在就让我们感受一下的Struts 2.0的魅力吧。
分别是关于项目中所使用的dwr的配置.table组件的配置说明文档.junit单元测试说明文档.还有我写这个例子时应该注意的一些东西.外加一些关于struts2和hibernate的技巧心得整理. 说一下这次3.0更新的内容.可能大家在我...
有关spring测试、struts测试、hibernate测试、软件测试、网站测试、利用JBuilder测试的技术
这两个参数与容器相关,如果想在servlet中作单元测试,则必须初始化这两个参数。 4、如果一个servlet中有很多个方法,则必须采用传递参数的形式,分解到每一个方法中。 2重构servlet 针对servlet以上的特点,我们...
花了好多时间搞了这个单元测试,在网上找相关的资料竟然都没有,我只有自己花时间搞了,搞这个ssh2,action的单元测试,花了好长时间,所有要5分了。