• 一个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. }  

         我们只要写一个继承类,重载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.  }  

           这样我们就解决了多个配置文件的问题,但是还有一个,我们一开始也不能确定有几个配置文件,这个方法还是不通用,需要再次重构,这样,我决定写一个方法,从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. }  

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

    分享到:
    评论
    3 楼 mmBlue 2010-05-08  
    搜索资料时查到此贴了,不知这个插件对注解的service属性有用没,目前项目中采用的EasyMock测试的。
    2 楼 mangoo1 2010-02-23  
    不应该把 原来的 setUp 方法 override,只需要重载 StrutsSpringTestCase 里面的 getContextLocations 就可以了。
    1 楼 wxlmcqueen 2010-01-19  
    试试,如果真能行,那真谢谢啦。
  • 相关阅读:
    1020.表-继承
    1019.模式(限定名)
    1018.行安全策略
    1017.权限
    1016.表结构修改
    1015.【转】oracle rowid and postgresql ctid
    1014.表-系统列
    20201227[java]同构字符串
    oCam_v4850录屏软件
    《软件定义网络中的异常流量检测研究进展》论文笔记
  • 原文地址:https://www.cnblogs.com/lexus/p/2340753.html
Copyright © 2020-2023  润新知