spring junit load应用程序上下文进行测试 [英] spring junit load application context for tests

查看:163
本文介绍了spring junit load应用程序上下文进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WEB-INF目录下有一些XML文件:

I've got some XML files under my WEB-INF directory:

  • lyricsBaseApp-servlet.xml
  • hibernate.xml
  • dataSource.xml
  • beans.xml

servlet xml导入其他xml文件:

the servlet xml imports other xml files:

<import resource="dataSource.xml"/>
<import resource="hibernate.xml"/>
<import resource="beans.xml"/>

我希望我的junit4 JukeboxTest类包含整个spring配置.使用默认文件名,我创建了一个JukeboxTest-content.xml文件.最后,我不知道该放在那里...

I would like my junit4 JukeboxTest class to include entire spring configuration. Using default filename I have created a JukeboxTest-content.xml file. And finally, I do not know what to put there...

我尝试过:

<import resource="/WEB-INF/dataSource.xml"/>
<import resource="/WEB-INF/hibernate.xml"/>
<import resource="/WEB-INF/beans.xml"/>

<import resource="classpath:./WEB-INF/dataSource.xml"/>
<import resource="classpath:./WEB-INF/hibernate.xml"/>
<import resource="classpath:./WEB-INF/beans.xml"/>

和其他一些想法,但都失败了.有人可以指出我如何访问这些文件以及spring解释这些文件路径的方式吗?

and some other ideas but all failed. Could someone point me how to access those files and what way spring interprets those filepaths?

推荐答案

选项1 (由于它是最佳做法,因此应首选):
WEB-INF下重构您的配置文件,并将公用部分(您也想从集成测试中访问)移到src/main/resources/.然后在src/test/resources/中编写特定于测试的配置文件(如果您只需要从src/main导入几个不同的配置文件以组装您的测试上下文,则跳过此操作,最好使用@ContextConfiguration).

Option 1 (should be preferred as it's the best practice):
Refactor your config files under WEB-INF and move the common parts (that you want to access also from integration tests) to src/main/resources/. Then write test specific configuration files in src/test/resources/ (if you only need to import several different config files from src/main to assemble your test context, then skip this, and use @ContextConfiguration preferably).

选项2 (黑客):
使用类似的引用:

Option 2 (hack):
Use references like:

@ContextConfiguration("file:src/main/webapp/WEB-INF/dataSource.xml")

选项3 (黑客):
如果您有Maven项目,则可以配置maven-surefire-plugin(用于测试阶段),以在测试执行期间将src/main/webapp声明为附加的类路径元素.

Option 3 (hack):
If you have a Maven project, you can configure the maven-surefire-plugin (used in the test phase) to declare src/main/webapp as an additional classpath element during test execution.

后两个选项被视为hack,因为src/main/webapp下的文件根本不应该位于类路径中.

The latter two options are considered as hack, because files under src/main/webapp are simply not supposed to be on the classpath.

之所以不能将这些文件称为classpath:/WEB-INF/*.xml,是因为它们确实不在类路径中.重要的是要了解您的Web应用程序是如何打包的,以及最终在类路径中结束的内容.假定默认的Maven项目结构为:

The reason why you can't refer to these files as classpath:/WEB-INF/*.xml is that they are indeed not on the classpath. It's important to understand how your webapp is packaged, and what exactly ends up on the classpath. Assuming a default Maven project structure:

  1. src/main/java中的Java类在编译后转到/WEB-INF/classes.
  2. 来自src/main/resources的资源也移至/WEB-INF/classes.
  3. 项目依赖项转到/WEB-INF/lib.
  4. src/main/webapp中的所有内容都转到/(程序包的根目录).当然,这意味着src/main/webapp/WEB-INF中的所有文件都将移至/WEB-INF.
  1. Java classes from src/main/java go to /WEB-INF/classes after compilation.
  2. Resources from src/main/resources go to /WEB-INF/classes as well.
  3. Project dependencies go to /WEB-INF/lib.
  4. Everything you have in src/main/webapp goes to / (root of the package). This means that all files from src/main/webapp/WEB-INF go to /WEB-INF, of course.

最重要的一点是,类路径将只包含/WEB-INF/classes/WEB-INF/lib中每个jar的一个条目.因此,对于类加载器,这两个位置之外的资源是完全不可见的.对于直接位于 /WEB-INF下的 xml配置文件也是如此,这就是为什么引用classpath:/WEB-INF/dataSource.xml将永远无法工作的原因.

The most important thing to know is that the classpath will only contain /WEB-INF/classes and one entry for each jar in /WEB-INF/lib. Consequently, resources outside these two locations are completely invisible for the classloader. This is also true for the xml config files directly under /WEB-INF, which is why the reference classpath:/WEB-INF/dataSource.xml will never work.

您可能会问自己,如果无法从类路径访问Spring,那么这些xml配置文件又会如何加载呢?答案很简单:启动webapp(而不是仅执行单元/集成测试)时,它运行在Servlet容器中,该容器可访问ServletContext(来自Servlet API的实际类),因此它使用ServletContext.getResourceAsStream()加载这些文件.理解的关键是来自此方法的javadoc :

You may ask yourself, how the hell are then these xml config files loaded by Spring if they are not reachable from the classpath? The answer is simple: When you start your webapp (as opposed to executing just unit/integration tests), it is running in a Servlet Container which provides access to the ServletContext (an actual class from the Servlet API), so it uses ServletContext.getResourceAsStream() to load these files. The key for understanding is the following quote from the javadoc of this method:

此方法不同于使用类加载器的java.lang.Class.getResourceAsStream.此方法允许Servlet容器从任何位置使servlet可以使用资源,而无需使用类加载器.

This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.

对不起,这变得太久了,但这就是整个故事……

Sorry this become way too long, but that's the whole story...

这篇关于spring junit load应用程序上下文进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆