使用Struts2的Junit Plugin测试Action是非常的方便的,如果在开发中采用TDD模式的话推荐使用。不说废话了,直入主题,已具体实例说话!
1、在原有的Struts2工程加入JUnit Plugin和它依赖的Spring包:
struts2-junit-plugin-2.1.8.jar
struts2-convention-plugin-2.1.8.jar org.springframework.core-3.0.3.RELEASE.jar org.springframework.test-3.0.3.RELEASE.jar
|
*补充:在Eclipse中要导入servlet-api.jar、jsp-api.jar包(转载者)。
2、新建个Action,我这里采用的Annotation的方式,可以实现零配置。TestAction.java
@Result(location = "/index.jsp") public class TestAction extends ActionSupport { private String param;
public String execute() { return SUCCESS; }
public String getParam() { return param; }
public void setParam(String param) { this.param = param; }
}
|
3、在工程中添加JUnit包(此步骤略了,搞不定这部的就不要看此篇文章啦!),创建TestCase,TestActionTest.java
import java.robinleex.action.TestAction;
import org.apache.struts2.StrutsTestCase; import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
public class TestActionTest extends StrutsTestCase {
@Test public void testExecute() throws Exception { ActionProxy proxy = null; TestAction test = null; request.setParameter("param", "test..."); proxy = getActionProxy("/test.action"); test = (TestAction)proxy.getAction();
String result = proxy.execute(); assertEquals("success", result); assertEquals("test...", test.getParam());
}
}
|
跑起来就可以了,非常好用,这个示例测试在Action当中传入一个参数并取出参数值。JUnit Plugin会模拟Servlet容器,模拟出HttPServletRequest,HttpServletResponse,ServletContext,PageContext...
转自:http://blog.chinaunix.net/u3/117605/showart_2292982.html
分享到:
相关推荐
Struts2 Spring 框架整合 单元测试 Action和Service http://blog.csdn.net/fansunion/
在搭建单元测试环境时,需要使用的 Jar 包有 junit4.jar 和 struts2-junit-plugin-2.1.8.jar。junit4.jar 是 Eclipse 自带的,可以在项目路径中导入。struts2-junit-plugin-2.1.8.jar 是 Struts2 提供的测试插件,...
struts2-junit-plugin-2.1.6.jar struts2测试 action测试 strutstestcase
struts2-junit-plugin-2.2.1.1.jar
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 ...
7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-spring-1.2.xsd(ehcache-spring-annotations...
关于ValueStack的描述: 1、 ValueStack其实就是一个放置Java对象的堆栈而已,唯一特别的是可以使用EL来获得值堆栈中...下面我们用一个雇员类为例,使用Junit框架(单元测试框架)来展示ValueStack的功能。 ......
B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言ModelAndView的内容 B.3 使用Spring进行综合测试 B.3.1 测试装配后的对象 B.3.2 综合测试事务处理对象 B.3.3 测试数据库 B.3.4 使用Gienah Testing在...
B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言ModelAndView的内容 B.3 使用Spring进行综合测试 B.3.1 测试装配后的对象 B.3.2 综合测试事务处理对象 B.3.3 测试数据库 B.3.4 使用Gienah Testing在...
11.Struts2自动查询目录下的action 35 12.一个Action处理多个业务 method 35 13.使用校验模板校验 35 14.校验模板 35 15.文件上传 36 Struts2 UI tabbed pane tab pane datetime tree 37 SSH Struts+Spring+...
12.5.2 使用hbm2java根据映射文件产生POJO 12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp ...
12.5.2 使用hbm2java根据映射文件产生POJO 12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp ...
b.2单元测试springmvc控制器 b.2.1模拟对象 b.2.2断言modelandview的内容 b.3使用spring进行综合测试 b.3.1测试装配后的对象 b.3.2综合测试事务处理对象 b.3.3测试数据库 b.3.4使用gienahtesting在junit4中...
12.5.2 使用hbm2java根据映射文件产生POJO 12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp ...
AngularJS.in.Action jquery in action中文版 JUnit in Action中文版 LuceneInAction Maven in action Redis in Action SPRING in action Struts2 in action 等等
三、JUnit测试错误 在使用JUnit/StrutsTest时,可能会遇到java.lang.UnsupportedClassVersionError: junit/framework/TestListener (Unsupported major.minor version 49.0)错误。 解决方法:根据JDK版本选择合适...
第一部份:介绍实例架构 结合实例讲解Web项目总体结构 分析多层结构优点 具体讲解每个层次的实现技巧 第二部分:介绍组织层次关联利器(Spring) 快速入门 ...借助Junit的单元测试 借助Ant的自动部署
struts2:使用struts2的jar包 * 项目体系分层: cn.itcast.elec.containner:自定义的spring容器,用于在控制层调用操作业务层 cn.itcast.elec.dao:项目的dao层,负责连接数据库的操作 cn.itcast.elec.daomain:...
* cn.itcast.crm.web.action:struts2的action * cn.itcast.crm.web.form:封装页面数据的javaBean * junit:开发人员测试用的 * 搭建hibernate层 * 定义需求:部门信息的页面数据要插入到数据库中 * 定义...
通过解决异常,提升了自身纠错能力,并且养成了边写边测的良好习惯,更加熟练的掌握了JUnit测试的使用。 深刻体会到Ajax的优势,异步发送请求处理与页面局部刷新,提高了系统的用户体验度。 通过使用工厂模式和面向...