在web应用中做action的单元测试,一向比较麻烦.因为它对web容器的依赖。现在有办法了。struts提供了 个测试插件,结合JUnit,可以很方便的进行单元测试,
一、加入struts2-junit-plugin.xxx.jar插件
二、测试类
/**
*
*/
package com.auth;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.auth.service.UserManager;
import com.auth.web.action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
/**
* @author llf
*
*/
public class UserActionTest extends StrutsSpringTestCase{
private ActionProxy proxy;
private UserAction action;
private UserManager manager;
@Override
public void setUp() throws Exception{
super.setUp(); //必须
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
manager = (UserManager)ctx.getBean("userManager");
}
@Test
public void testAction() throws Exception{
proxy = this.getActionProxy("/user_list.action");
action = (UserAction)proxy.getAction();
action.setManager(manager);
String result = action.list();
assertEquals("success",result);
}
}
三、注意
1、strut2-junit-plugin共提供了两个基类:
StrutsTestCase:供普通action继承
StrutsSpringTestCase:供ssh环境中的action继承
2、在ssh环境中一定要super.setUp()
3、action中的注入可以手动用setXxx()方法实现
分享到:
相关推荐
测试struts2中的action
Struts2 Spring 框架整合 单元测试 Action和Service http://blog.csdn.net/fansunion/
Struts 2实战 struts2 in Action
struts2一个action处理多个请求
Struts 2 Action不再依赖于Servlet API,从而允许Action脱离Web容器运行,从而降低了测试Action的难度。 当然,如果Action需要直接访问HttpServletRequest和HttpServletResponse参数,Struts 2 Action仍然可以访问...
Struts2 Action参数详细说明,很有用的。
如果你想自学struts2,此文档是很好的自学宝典,经典struts2 in Action,能帮你建立整体的struts2思想,你还在犹豫什么
Struts2 in action 中文版
第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...
struts2 in action 源码
Struts 2实战,Struts 2 in action 的中文版,pdf格式,讲的是Struts 2。此书是Struts 2的一本很经典的书。花几块钱在网上买的。。。。
struts2 Action 电子书 下载
Struts2inAction 英文版 非常实用的一本Struts教材
Struts2 实战 Struts 2 in Action
struts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 Action 包 命名空间 servlet APIstruts2 ...
Struts2 In Action中文版,完整PDF。
1、在struts2的action中可以通过实现ServletResponseAware/ServletResquestAware接口 (org.apache.struts2.inteceptor.ServletResponseAware/ServletResquestAware)直接访问 HttpServletResponse/HttpServletRequest...
struts2如何实现弹出action返回的错误信息
action跳转后的页面接收由上一个ACTION传过来的参数
从jsp页面提交form表单后,在struts.xml中寻找匹配的action。在action中处理完业务后返回,在struts.xml中的result属性中转向到另外一个action。