spring junit 加载应用程序上下文以进行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14946298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring junit load application context for tests
提问by ducin
I've got some XML files under my WEB-INF directory:
我的 WEB-INF 目录下有一些 XML 文件:
- lyricsBaseApp-servlet.xml
- hibernate.xml
- dataSource.xml
- beans.xml
- 歌词BaseApp-servlet.xml
- 休眠文件
- 数据源文件
- bean.xml
the servlet xml imports other xml files:
servlet xml 导入其他 xml 文件:
<import resource="dataSource.xml"/>
<import resource="hibernate.xml"/>
<import resource="beans.xml"/>
I would like my junit4 JukeboxTestclass to include entire spring configuration. Using default filename I have created a JukeboxTest-content.xmlfile. And finally, I do not know what to put there...
我希望我的 junit4JukeboxTest类包含整个 spring 配置。使用默认文件名我创建了一个JukeboxTest-content.xml文件。最后,我不知道该放什么...
I've tried:
我试过了:
<import resource="/WEB-INF/dataSource.xml"/>
<import resource="/WEB-INF/hibernate.xml"/>
<import resource="/WEB-INF/beans.xml"/>
or
或者
<import resource="classpath:./WEB-INF/dataSource.xml"/>
<import resource="classpath:./WEB-INF/hibernate.xml"/>
<import resource="classpath:./WEB-INF/beans.xml"/>
and some other ideas but all failed. Could someone point me how to access those files and what way spring interprets those filepaths?
和其他一些想法,但都失败了。有人可以指出我如何访问这些文件以及 spring 解释这些文件路径的方式吗?
回答by zagyi
Option 1(should be preferred as it's the best practice):
Refactor your config files under WEB-INFand 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/mainto assemble your test context, then skip this, and use @ContextConfigurationpreferably).
选项 1(应该是首选,因为它是最佳实践):
重构您的配置文件,WEB-INF并将公共部分(您还想从集成测试访问)移动到src/main/resources/. 然后编写测试特定的配置文件src/test/resources/(如果您只需要从中导入几个不同的配置文件src/main来组装您的测试上下文,则跳过此步骤,@ContextConfiguration最好使用)。
Option 2(hack):
Use references like:
选项 2(hack):
使用如下参考:
@ContextConfiguration("file:src/main/webapp/WEB-INF/dataSource.xml")
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/webappas an additional classpath element during test execution.
选项3(hack):
如果你有一个Maven项目,你可以配置maven-surefire-plugin(在测试阶段使用)src/main/webapp在测试执行期间声明为一个额外的类路径元素。
The latter two options are considered as hack, because files under src/main/webappare simply not supposed to be on the classpath.
后两个选项被认为是 hack,因为下面的文件src/main/webapp根本不应该在类路径上。
Now the detailed explanation:
现在详细解释:
The reason why you can't refer to these files as classpath:/WEB-INF/*.xmlis 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:
您不能引用这些文件的原因classpath:/WEB-INF/*.xml是它们确实不在类路径中。了解您的 web 应用程序是如何打包的,以及最终在类路径上的确切内容非常重要。假设一个默认的 Maven 项目结构:
- Java classes from
src/main/javago to/WEB-INF/classesafter compilation. - Resources from
src/main/resourcesgo to/WEB-INF/classesas well. - Project dependencies go to
/WEB-INF/lib. - Everything you have in
src/main/webappgoes to/(root of the package). This means that all files fromsrc/main/webapp/WEB-INFgo to/WEB-INF, of course.
- Java类从
src/main/java转到/WEB-INF/classes编辑后。 - 资源也来自
src/main/resourcesgo/WEB-INF/classes。 - 项目依赖转到
/WEB-INF/lib. - 您拥有的所有内容都
src/main/webapp转到/(包的根目录)。当然,这意味着所有文件都src/main/webapp/WEB-INF转到/WEB-INF。
The most important thing to know is that the classpath will only contain /WEB-INF/classesand 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.xmlwill never work.
要知道,最重要的是,类路径将只包含/WEB-INF/classes并在每个罐子一个条目/WEB-INF/lib。因此,这两个位置之外的资源对于类加载器是完全不可见的。对于直接位于 下的 xml 配置文件也是如此/WEB-INF,这就是引用classpath:/WEB-INF/dataSource.xml永远不会起作用的原因。
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 javadocof this method:
您可能会问自己,如果无法从类路径访问这些 xml 配置文件,Spring 怎么会加载它们呢?答案很简单:当您启动 web 应用程序时(而不是仅执行单元/集成测试),它运行在一个 Servlet 容器中,该容器提供对ServletContext(来自 Servlet API 的实际类)的访问,因此它用于ServletContext.getResourceAsStream()加载这些文件。理解的关键是这个方法的javadoc的以下引用:
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.
此方法与 java.lang.Class.getResourceAsStream 不同,后者使用类加载器。此方法允许 servlet 容器从任何位置为 servlet 提供资源,而无需使用类加载器。
Sorry this become way too long, but that's the whole story...
对不起,这变得太长了,但这就是整个故事......
回答by Kris
try this
尝试这个
@ContextConfiguration(locations = {"classpath:**/dataSource.xml",
"classpath:**/hibernate.xml",
"classpath:**/WEB-INF/beans.xml"})

