在使用StrtusSpringTestCase时调用getActionProxy时抛nullpointer的原因
给我这孱弱的博客添一篇文章吧……
=========================================================================
最近没事想玩一下Struts2的单元测试,然后查了下一般可以用StrutsSpringTestCase来做,这个类继承自StrutsTestCase。在网上搜了下简单的例子。
因为struts测试库是模拟出request等web环境,所以不需要跑在tomcat里,可以直接run junit。模拟action的第一步就是调用getActionProxy()方法。如:
一瞬间蛋都碎了……
classpath*:applicationContext.xml
spring默认读取/WEB-INF下的app...xml,居然TestCase默认读取classpath下…… 而我的app...xml放在了默认的/WEB-INF/下。那当然创建不了自己定义的bean了。此时我试了一下,将app...xml搬到了类路径下,果然就成功了。
本来这样就想算了,后来想想,能不能把StrutsSpringTestCase配置成读取我自定义的路径。protected void setupBeforeInitDispatcher() throws Exception { // only load beans from spring once if (applicationContext == null) { GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader(); applicationContext = xmlContextLoader.loadContext(getContextLocations()); } ........}protected String[] getContextLocations() { return new String[] {DEFAULT_CONTEXT_LOCATION}; }
这里加载上下文的方法loadContext直接读取了那个默认的静态变量DEFAULT_CONTEXT_LOCATION,也就是"classpath*:applicationContext.xml"。而loadContext方法的实现不断深入,关于路径变量locations都是一些其他的判断和字符上的改变,没有我预想中的如果判断为空,再怎么怎么。换句话说,loadContext方法处已经将最终的路径给定死了。而且似乎没有去读web.xml中的内容(这毕竟不是真的web容器),所以我即使在web.xml中配置了路径也没有生效。
其实最后两点我还是对自己有点怀疑。觉得这struts测试lib也不能hardcode成这样,默认是好事。。也得可配,至少读一下web.xml吧