项目布局和弹簧测试支持问题
我们的项目布局如下。
src
src/test/java
src/test/resources
并且我们无法添加 src/main/(java, resources) 代码,因为早期开发尚未完成。
src/test/java 在与 src 文件夹中的被测类相同的包中进行单元测试和集成测试。
单元测试在当前设置中运行良好,但运行集成测试时出现问题。
当测试类和配置文件与被测类一起位于 src 文件夹中且与类相同的包中时,集成测试运行得非常好。
但是当我将测试类放入 src/test/java 并将配置文件放入 src/test/resources 时,由于上下文初始化问题,测试无法运行。
请注意以下有关环境设置的信息
所有 src 、 src/test/java 和 src/test/resources 的 1 个构建输出文件夹只是 src 文件夹。
2 我正在使用 classpath*: 指定配置位置,否则 spring 无法在资源文件夹中找到配置文件。
@ContextConfiguration(locations={"classpath*:applicationContext_getCorpAcctPrefDetailsSP.xml"})
3 为测试类尝试了 @Autowired 和基于 setter 的 DI
> (i). in case of @Autowired i get error for depedency saying
No unique bean of type GetCorpAccountPreferencesDetailsSP is defined expected at least 1 matching bean
also i am using base package scan
> (ii). in case of Setter based DI context get initialized and unit test run but all the dependencies injected are null in test class.
请解释问题的原因和解决方案。
因为当集成测试与被测类一起位于 src 文件夹中时,一切工作正常。
我怀疑当 spring 创建上下文作为被测类与测试不在同一源文件夹中时,不同的源文件夹(src 和 test)会创建问题。
谢谢 恩巴蒂
We have project layout as below.
src
src/test/java
src/test/resources
and we cant add src/main/(java, resources) for code, because of earlier developemnt done.
src/test/java is having both unit and integration tests in same package as class under test has in src folder.
unit tests are running fine in current setup but issues are with running integration tests.
integration tests run perfectly fine when test class and configuration files are along side class under test, in src folder and same package as class.
but when i put test class in src/test/java and config files in src/test/resources test fails to run because of context initialization issues.
please note following about env setup
1 build output folder for all the src , src/test/java and src/test/resources is src folder only.
2 i am using classpath*: to specify config location, as otherwise spring fails to locate config file in resource folder.
@ContextConfiguration(locations={"classpath*:applicationContext_getCorpAcctPrefDetailsSP.xml"})
3 tried both @Autowired and setter based DI for test classes
> (i). in case of @Autowired i get error for depedency saying
No unique bean of type GetCorpAccountPreferencesDetailsSP is defined expected at least 1 matching bean
also i am using base package scan
> (ii). in case of Setter based DI context get initialized and unit test run but all the dependencies injected are null in test class.
please expalin what can be reason for issue and any solution.
As everything is working fine when integration tests are in src folder alongside class under test.
i suspect differect source folders (src and test)creating issue when spring create context as class under test is not in same source folder as test.
thanks
nBhati
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在运行时,Spring 并不关心(或知道)原始源代码位于哪个文件夹中。重要的是类路径 - 哪些编译文件和哪些资源文件夹放在类路径上。如果您在运行测试时收到有关无法找到 XML 文件的错误,则强烈表明在测试运行时这些 XML 文件不在类路径中。
At run-time Spring doesn't care (or know) which folder your original source code is in. All that matters is the classpath - which compiled files and which resource folders are being put on the classpath. If you are getting errors about XML files that cannot be found when you run your tests, that strongly suggests that those XML files are not on the classpath when the tests run.