一个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的原代码,代码如下:

Java代码 复制代码  收藏代码
  1. public abstract class StrutsSpringTestCase extends StrutsTestCase {   
  2.     private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:applicationContext.xml";   
  3.     protected static ApplicationContext applicationContext;   
  4.   
  5.   
  6.     protected void setupBeforeInitDispatcher() throws Exception {   
  7.         //init context   
  8.         GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();   
  9.         applicationContext = xmlContextLoader.loadContext(getContextLocations());   
  10.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);   
  11.     }   
  12.   
  13.     protected String getContextLocations() {   
  14.         return DEFAULT_CONTEXT_LOCATION;   
  15.     }   
  16. }  
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,我们就可以这样写:

Java代码 复制代码  收藏代码
  1. @Override  
  2.  protected void setupBeforeInitDispatcher() throws Exception {   
  3.      GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();   
  4.      applicationContext = xmlContextLoader.loadContext(new String[]{"applicationContext.xml,","applicationContext-other.xml,"});   
  5.      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);   
  6.  }  
   @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开通的文件组成一个字符串数组,这样就不需要修改改类了。代码如下:

Java代码 复制代码  收藏代码
  1. public class SpringBeanFactoryMock extends StrutsSpringTestCase {   
  2.     @Override  
  3.     public void setUp() throws Exception {   
  4.         super.setUp();   
  5.     }   
  6.     @Override  
  7.     protected void setupBeforeInitDispatcher() throws Exception {   
  8.         GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();   
  9.         applicationContext = xmlContextLoader.loadContext(getContextLocation());   
  10.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);   
  11.     }   
  12.     public String[] getContextLocation(){   
  13.         URL classUrl = SpringBeanFactoryMock.class.getResource("");   
  14.         String path = classUrl.getPath();   
  15.         try {   
  16.             path = URLDecoder.decode(path, "UTF-8");   
  17.         } catch (UnsupportedEncodingException e) {   
  18.             e.printStackTrace();   
  19.         }   
  20.         path =  path.substring(1 , path.indexOf("WEB-INF"))+ "WEB-INF/";   
  21.         File configPath = new File(path);   
  22.         String[] applicationContexts = configPath.list(new FilenameFilter(){   
  23.             public boolean accept(File dir, String name){   
  24.                 if(name.toLowerCase().startsWith("applicationcontext")){   
  25.                     return true;   
  26.                 }   
  27.                 return false;   
  28.             }                  
  29.         });            
  30.         for(int i=0;i<applicationContexts.length;i++){   
  31.             applicationContexts[i] = "file:"+path +  applicationContexts[i];   
  32.         }   
  33.         return applicationContexts;   
  34.     }   
  35. }  
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  
试试,如果真能行,那真谢谢啦。
posted on 2012-02-06 23:52  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/02/06/2340753.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值