最近在学习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;
- }
- }
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);
- }
@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;
- }
- }
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类就可以了。
评论