[英]can not load Spring application context from tests
I have the next project structure: 我有下一个项目结构:
src/
main/
java/
DAO/Dao classes
data/data classes
resources/
application-context.xml
test/
java/
data/ Test files
resources/
application-context-test.xml
When I load context outside tests i use: 当我在测试之外加载上下文时,我使用:
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
and it works.And if use 它可以工作。如果使用
@ContextConfiguration(locations = {"classpath:application-context.xml"})
inside tests - it works. 内部测试-它有效。 But if I use for:
但是如果我用于:
@ContextConfiguration(locations = {"classpath:application-context-test.xml"})
I got exception: 我有例外:
java.lang.IllegalStateException: Failed to load ApplicationContext
Any ideas why? 有什么想法吗? Maybe something is wrong with XML file?
也许XML文件有问题?
Make sure both your main and test directories are set up as source directories in eclipse. 确保将主目录和测试目录都设置为eclipse中的源目录。 You can do this from the ui, but you can also open the hidden .classpath file at the root of your application source.
您可以从ui中执行此操作,但也可以在应用程序源的根目录下打开隐藏的.classpath文件。 The beginning should look something like what you see before where the paths match your file structure:
开头应该看起来像您在路径匹配文件结构之前看到的内容:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/resources"/>
.
.
.
</classpath>
If it does, the tests should work. 如果是这样,则测试应该可以进行。 If not they won't.
如果没有,他们不会。
You can do this from the UI by right clicking on the src/main/resources directory and selecting Build Path >> Add to Build Path. 您可以通过右键单击src / main / resources目录并选择Build Path >> Add to Build Path,从UI中执行此操作。 Your package explorer should look something like this with each of the source directories at the top.
您的程序包浏览器应该看起来像这样,每个源目录都在顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.