`
zhangjunji111
  • 浏览: 46705 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个Struts2结合Spring测试的方法

阅读更多

  最近在学习Struts2,进行测试驱动开发,对于struts2与Spring集成的测试,Struts2给出了一个插件struts2-junit-plugin-2.1.8.1.jar,这个插件需要spring-test.jar包的支持,所有需要测试的Action类都继承StrutsSpringTestCase。这个测试要求只能用个application.xml文件,而且必须放到类路径根目录下面。在我们开发中往往编写许多以application开头的部署文件,一般也不在类路径下面存放,例如我一般习惯在WEB-INF目录下建立一个config文件夹,将spring的部署文件都放到config文件夹下面。但是用StrutsSpringTestCase类不能解决改问题,我查看了一下StrutsSpringTestCase的原代码,代码如下:

public abstract class StrutsSpringTestCase extends StrutsTestCase {
    private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:applicationContext.xml";
    protected static ApplicationContext applicationContext;


    protected void setupBeforeInitDispatcher() throws Exception {
        //init context
        GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
        applicationContext = xmlContextLoader.loadContext(getContextLocations());
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    }

    protected String getContextLocations() {
        return DEFAULT_CONTEXT_LOCATION;
    }
}

     我们只要写一个继承类,重载setupBeforeInitDispatcher()方法就可以解决改问题了,如果我们有两个配置文件applicationContext.xml、applicationContext-other.xml,我们就可以这样写:

   @Override
    protected void setupBeforeInitDispatcher() throws Exception {
        GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
        applicationContext = xmlContextLoader.loadContext(new String[]{"applicationContext.xml,","applicationContext-other.xml,"});
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    }

       这样我们就解决了多个配置文件的问题,但是还有一个,我们一开始也不能确定有几个配置文件,这个方法还是不通用,需要再次重构,这样,我决定写一个方法,从config文件下读取文件,所有以application开通的文件组成一个字符串数组,这样就不需要修改改类了。代码如下:

public class SpringBeanFactoryMock extends StrutsSpringTestCase {
	@Override
	public void setUp() throws Exception {
		super.setUp();
	}
	@Override
    protected void setupBeforeInitDispatcher() throws Exception {
        GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
        applicationContext = xmlContextLoader.loadContext(getContextLocation());
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    }
	public String[] getContextLocation(){
		URL classUrl = SpringBeanFactoryMock.class.getResource("");
		String path = classUrl.getPath();
		try {
			path = URLDecoder.decode(path, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		path =  path.substring(1 , path.indexOf("WEB-INF"))+ "WEB-INF/";
		File configPath = new File(path);
		String[] applicationContexts = configPath.list(new FilenameFilter(){
			public boolean accept(File dir, String name){
				if(name.toLowerCase().startsWith("applicationcontext")){
					return true;
				}
				return false;
			}				
		});			
		for(int i=0;i<applicationContexts.length;i++){
			applicationContexts[i] = "file:"+path +  applicationContexts[i];
		}
		return applicationContexts;
	}
}

 

    以后我们测试Struts2的Action类时候,只要继承SpringBeanFactoryMock类就可以了。

分享到:
评论
3 楼 mmBlue 2010-05-08  
搜索资料时查到此贴了,不知这个插件对注解的service属性有用没,目前项目中采用的EasyMock测试的。
2 楼 mangoo1 2010-02-23  
不应该把 原来的 setUp 方法 override,只需要重载 StrutsSpringTestCase 里面的 getContextLocations 就可以了。
1 楼 wxlmcqueen 2010-01-19  
试试,如果真能行,那真谢谢啦。

相关推荐

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    spring和struts的整合-aop的使用

    通过修改相关配置文件位置,和导入的架包,终于修复了一些错误,实现了spring和struts2的完美结合,并使用了spring的aop和日志功能,本人亲自测试过,没问题,方便下载使用

    struts2+hibernate3+spring2资源包

    该文件是struts2+hibernate3+spring2结合整理过后通过测试的相关包文件。

    深入浅出struts2

    最后,依赖注入也成了Struts2王国中的一等公民,这项功能是通过Spring框架的插件和Plexus共同提供的,与PicoContainer的结合工作还正在进行中。 本书的目的,是为了帮助读者掌握Struts2框架,并能够对组成框架的功能...

    Spring与Struts结合测试的问题解答

    NULL 博文链接:https://zhangjunji111.iteye.com/blog/428212

    基于STRUTS框架的网上订房系统论文

    然后在分析了Struts框架技术的基本方法的基础上,实现了一个基于Struts框架的网上宾馆订房系统。最后,利用LoadRunner测试工具对其进行性能测试。测试结果表明,本文所设计的宾馆网上订房系统能够满足宾馆宣传、网上...

    基于Struts、Spring、Hibernate考核管理系统的研究与实现

    通过对考核管理现状的分析,结合Struts、Spring、Hibernate框架开发系统的优势,设计并实现了基于Struts、Spring、Hibernate框架的考核管理系统,并经全面测试和试运行。验证了管理系统的各项功能,系统操作简单、快捷,...

    基于Struts+ Spring+ Hibernate的高校学生管理系统的设计与实现

    结合学校的实际情况,采用B/S开发模式的多层体系方法,应用轻量级的Java EE框架技术(Struts + Spring + Hibernate),将系统划分为表示层、业务逻辑层、数据持久层,详细设计并实现了学生管理系统的相应功能,并...

    Spring 2.0 开发参考手册

    9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...

    Spring MVC 3.2.10 + Mybatis3.2.7+Mysql

    index.jsp首页,进去有两个提交,查询都是一样的,当时是用的MapperFactoryBean,只能一个个的配置mapper,所以后来改为MapperScannerConfigurer,这就是我写两个同样查询类做测试的原因。 所有的配置文件里面,我...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring JDBC包结构...

    低清版 大型门户网站是这样炼成的.pdf

    (Struts 2+Spring 2+Hibernate 3).pdf(完整版) 网上有高清版350M的。可以去下 http://115.com/file/be5gwid8 请于下载后 24H 内及时删除!请抱着学习的态度下载此资料。 总共900多页!!!!!!! 第1篇 ...

    Spring中文帮助文档

    为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一个LoadTimeWeaver 4. 资源 4.1. 简介 4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPath...

    Spring API

    为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一个LoadTimeWeaver 4. 资源 4.1. 简介 4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPath...

    spring chm文档

    9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...

    spring快速入门教程

    编写功能性测试。 配置Hibernate和Transaction。 载入Spring的applicationContext.xml文件。 设置业务代理(business delegates)和DAO的依赖性。 把spring写入Struts 程序。...如何结合Hibernate和Struts

    基于java的struts + spring+hibernate的题库及试卷管理模块的设计与开发(源代码+论文).zip

    试卷的生成可能根据多种条件来生成,可以人工出题,自动出题,试卷应具有“单元测试、学年/半学年测试、学年考试、年级统考等多种性质,以适合不同场合的需要。可以由出卷人来设定,试卷生成与题型管理结合非常紧密...

    源码基于JSP的企业级新闻系统(SSH+MYSQL).rar

    这个JSP源码是一个基于Struts2、Spring和Hibernate(SSH)框架,结合MySQL数据库的企业级新闻系统。整个系统采用MVC模式设计,实现了新闻的发布、编辑、删除、查询等功能,同时支持用户注册、登录和评论等互动功能。...

    BOS 技术整理

    struts2 的 Action 使用 spring 注解,被 spring 管理 @Controller spring 表现层 bean @Scope 配置 Action 为多例 Spring Data JPA 代替了 Hibernate "CURD " 但是和Hibernate一样 实现DAO(数据)层的功能 "增...

    javawebtest

    就以前做的一个项目为例, 这个项目是以Struts+Spring+iBatis为框架扩展起来的一个Java Web项目,最初只是用JUnit对业务层的Business Class进行测试,后来逐步扩展到演示层和持久层.并结合了CruiseControl 进行持续集成...

Global site tag (gtag.js) - Google Analytics