Struts2单元测试-1(使用struts2-junit-plugin)
来源:互联网 发布:php模拟登录教务处 编辑:程序博客网 时间:2024/06/05 11:25
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、 具体例子:
//spring的测试方式public class UserActionTest extends StrutsSpringTestCase { @Autowired // 在执行每个test之前,都执行setUp; @Before public void setUp() { try { super.setUp(); } catch (Exception e) { e.printStackTrace(); }} // 在执行每个test之后,都要执行tearDown @After public void tearDown() {} //这个是单元测试方法,一个方法测试一个功能或者一个action /* * request是类StrutsSpringTestCase的成员变量,是MockHttpServletRequest对象 * 在这里mock出来的一个web中的一个request * 通过,request.setParameter("userId", "1");来模拟web传入的参数 * 通过executeAction("/user/*Action!*.action");来模拟调用web链接 */ @Test//注意了,这里需要以test作为测试方法的前缀,否则会出现找不到测试方法的异常,但是对于没有继承StrutsSpringTestCase的测试类只要上面有@Test即可。 public void testRegisterUser() throws UnsupportedEncodingException, ServletException { request.setParameter("username", "gsdhaiji_cai"); String result = executeAction("/user/user!registerUser.action"); assertEquals("\"{}\"", result);}//其实这里的判断是根据action返回的response里面的数据,并不是action中的return 后面的那个字符串。}}
- Struts2单元测试-1(使用struts2-junit-plugin)
- Struts2单元测试-1(使用struts2-junit-plugin)
- struts2.1之后的Junit plugin使用
- struts2 junit plugin的使用
- Struts2单元测试 使用Junit测试Action
- Struts2+Spring+Junit单元测试
- 使用struts2 - junit - plugin - *.jar对Action进行测试
- struts2-json-plugin 使用
- Struts2 JQuery Plugin 使用
- struts2-json-plugin使用
- struts2-json-plugin 使用
- Struts2 convention plugin (1)
- 使用 Struts2 JSON plugin ( Struts2 + jQuery )
- 使用 Struts2 JSON plugin ( Struts2 + jQuery )
- 单元测试(junit使用)
- Struts2,AJAX,json-plugin使用
- Struts2 Rest Plugin的使用
- Struts2 convention-plugin 使用笔记
- 淡然面对生活
- 企业应用系统安全体系包括哪些内容?
- linux device tree源代码解析
- opencv 3.0 Mat STL 迭代器的使用
- Linux应用程序地址布局
- Struts2单元测试-1(使用struts2-junit-plugin)
- js window.onload 加载多个函数
- 闪现的文字
- 面向对象剪刀石头布小游戏的制作
- ImageView 的 scaleType 小结
- HDU 3974 Assign the task(并查集)
- 2015年 计划
- 互联网体系将保障企业的信息安全 !
- Chapter08-用户模式下的线程同步