JUnit:在 @ContextConfiguration 中找不到我的 XML 文件位置
这是我的 JUnit 类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "WEB-INF/spring-test-config.xml"})
public class TestXXXX extends TestBase
{ ...
当我使用 JUnit 运行程序(来自 Eclipse)启动此测试类时,它失败了,因为找不到文件 spring-test-config.xml
。
我的项目架构是:
/src/main/com/xxx/
:我的源代码/src/main/WEB-INF/
:我的配置文件;在此文件夹下有spring-test-config.xml
:该文件不是最终的 xml,因为它包含一些由 Ant 目标替换的标记。该文件的最终结果位于WebContent
(实际上是部署目录) - 参见上文。/WebContent/WEB-INF/classes/com/xxx/
:我的二进制代码/WebContent/WEB-INF/spring-test-config.xml
那么,我该如何解决这个问题呢?
This is my JUnit class :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "WEB-INF/spring-test-config.xml"})
public class TestXXXX extends TestBase
{ ...
When I launch this test class with JUnit runner (from Eclipse), it's failed, because the file spring-test-config.xml
is not found.
My project architecture is :
/src/main/com/xxx/
: my source code/src/main/WEB-INF/
: my config files; under this folder there isspring-test-config.xml
: this file is not the final xml because it contains some tokens which replace by a Ant target. And the final result of this file is place inWebContent
(deploy directory in fact) - see above./WebContent/WEB-INF/classes/com/xxx/
: my binary code/WebContent/WEB-INF/spring-test-config.xml
So, how can i do to resolve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否展示您所有的项目结构,以及您的测试类放在哪里。
从 eclipse 配置类路径中的目录。
之后,您可以在 src/resources/test 目录中定义 spring-context 应用程序文件,并使用 @ContextConfiguration(locations = { "/spring-test-config.xml"}) 加载它。
我希望它有帮助
Can you show all your project structure, and where is your test class placed too.
Configure directories in classpath from eclipse.
After that you can define your spring-context application file in
src/resources/test
directory and load it with@ContextConfiguration(locations = { "/spring-test-config.xml"})
.I hope it helps
我以前遇到过这个问题 - 您需要将 WEB-INF 文件夹添加到构建路径中。
I've faced this problem before - you need to add the WEB-INF folder to your build path.
您可能想检查您的 ant 配置。
我遇到了同样的问题(使用 Maven)并尝试了 Ricardo 和 Katie 的想法无济于事,然后我将一个测试配置文件放入 /src/test/resources (注意细微的区别),然后才执行 '@ContextConfiguration(locations = { “/spring-test-config.xml”})'工作。总之,您可能想要遵循里卡多的想法,然后检查您的构建过程。
You would probably want to check your ant configuration.
I had the same problem (using Maven) and tried Ricardo's and Katie's ideas no to avail, then I put a test config file into /src/test/resources (note the subtle difference) and only then did '@ContextConfiguration(locations = { "/spring-test-config.xml"})' work. In conclusion, you probably want to go with Ricardo's idea and then check your build process.