`
dky_rl
  • 浏览: 66969 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类
最新评论

Struts2 Action Junit 测试

阅读更多

 

记得刚学struts2时就了解了点struts2 的 action的测试方法,但是一直都没针对Action写过测试,最近稍微研究了下struts2 的 action测试,遇到了很多问题:

 

struts 2.0 之前

 

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.StrutsTestCase;
import org.junit.Test;

import com.jack.lucene.service.LuceneSearchService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.DefaultActionProxyFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.inject.Container;

/**
 * @author jack
 * 
 */
public class LuceneSearchHandlerTest extends StrutsTestCase {

	/**
	 * Test method for {@link com.jack.lucene.handler.LuceneSearch#execute()}.
	 * @throws Exception 
	 */
	@Test
	public void testExecute() throws Exception {
		Map paramMap = new HashMap();
		paramMap.put("keyWord", "grails 参考");
		Map context = new HashMap();
		context.put(ActionContext.PARAMETERS, paramMap);
		ConfigurationManager cm = new ConfigurationManager();
		Configuration conf = cm.getConfiguration();
		Container containter = conf.getContainer();
		DefaultActionProxyFactory actionProxyFactory = new DefaultActionProxyFactory();
		actionProxyFactory.setContainer(containter);
		ActionProxy proxy = actionProxyFactory.createActionProxy("/lucene","luceneSearch", context);
		LuceneSearch lsh = (LuceneSearch) proxy.getAction();
		lsh.setLuceneSearchService(new LuceneSearchService());
		String result = lsh.execute();
		
		assertEquals("success", result);
		

	}

}

 

 但是如果你的项目是Spring管理的那么执行测试会报错:

 要想测试只能暂时不用Spring去管理。或者用其他方法

 

SEVERE:   [26:24.218] ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app! 
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml: 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 

struts2.1 之后 请看这几篇文章

 

http://kang36897.blog.163.com/blog/static/170473732010710101238126/

 

http://jzbgjsy.iteye.com/blog/736162

 

http://www.iteye.com/topic/572614

 

http://friping.iteye.com/blog/487673

 

 

分享到:
评论
1 楼 xianqi_h 2012-08-20  
请问用什么办法呢, 请指教呀

相关推荐

    Struts2SpringUnitDemo单元测试

    Struts2 Spring 框架整合 单元测试 Action和Service http://blog.csdn.net/fansunion/

    struts2-junit-plugin-2.2.1.1.jar

    struts2-junit-plugin-2.2.1.1.jar

    struts2-junit-plugin-2.1.6.jar

    struts2-junit-plugin-2.1.6.jar struts2测试 action测试 strutstestcase

    Struts2+Spring+hibernate中对action的单元测试环境搭建[总结].pdf

    Struts2+Spring+hibernate中对action的单元测试环境搭建[总结].pdf

    Struts2 in action中文版

    13.2.2 JUnit与测试 282 13.2.3 测试validation.xml文件 284 13.3 最大化重用 284 13.3.1 使用component标签组件化 285 13.3.2 重用模板化的标签 286 13.3.3 连接UI标签和对象 287 13.4 高级UI标签的使用 288 13.4.1...

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...

    Struts2中关于ValueStack的一些操作

    关于ValueStack的描述: 1、 ValueStack其实就是一个放置Java对象的堆栈而已,唯一特别的是可以使用EL来获得值堆栈中...下面我们用一个雇员类为例,使用Junit框架(单元测试框架)来展示ValueStack的功能。 ......

    struts2.1宝典

    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+...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    Spring in Action(第二版 中文高清版).part2

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    strutsTestCase所需要用到的jar

    strutsTestCase所需要用到的jar org.springframework.core-3.1.2.RELEASE.jar ...struts2-junit-plugin-2.3.16.3.jar xmlbeans-2.3.0.jar xmlpull-1.1.3.1.jar xstream-1.4.2.jar xwork-core-2.3.16.3.jar

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    Spring in Action(第二版 中文高清版).part1

    16.2 协同使用Spring和WebWork 2/Struts 2 16.3 集成Spring和Tapestry 16.3.1 集成Spring和Tapestry 3 16.3.2 集成Spring和Tapestry 4 16.4 协同使用Spring和JSF 16.4.1 解析JSF管理的属性 16.4.2 解析Spring...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    in Action 集合

    AngularJS.in.Action jquery in action中文版 JUnit in Action中文版 LuceneInAction Maven in action Redis in Action SPRING in action Struts2 in action 等等

    客户关系管理系统框架搭建(二)

    * cn.itcast.crm.web.action:struts2的action * cn.itcast.crm.web.form:封装页面数据的javaBean * junit:开发人员测试用的 * 搭建hibernate层 * 定义需求:部门信息的页面数据要插入到数据库中 * 定义...

    SSH 项目框架搭建总结

    struts2:使用struts2的jar包 * 项目体系分层: cn.itcast.elec.containner:自定义的spring容器,用于在控制层调用操作业务层 cn.itcast.elec.dao:项目的dao层,负责连接数据库的操作 cn.itcast.elec.daomain:...

    Spring in Action(第2版)中文版

    16.2协同使用spring和webwork2/struts2 16.3集成spring和tapestry 16.3.1集成spring和tapestry3 16.3.2集成spring和tapestry4 16.4协同使用spring和jsf 16.4.1解析jsf管理的属性 16.4.2解析springbean 16.4.3...

    客户关系管理系统框架搭建

    客户关系管理系统框架搭建: crm项目的架构 * 创建web工程 * 引入jar包 ... * cn.itcast.crm.web.action:struts2的action * cn.itcast.crm.web.form:封装页面数据的javaBean * junit:开发人员测试用的

    dangdang和smartstruts2.rar

    Struts2、JDBC+连接池(dbcp)、jQuery+Ajax、javamail、log4j、junit、MySQL 系统架构: 表现层:jQuery+Ajax、Struts2标签+OGNL 控制层:Struts2控制器,Action组件 业务层:JavaBean组件、javamail 数据访问层...

Global site tag (gtag.js) - Google Analytics