简体   繁体   English

项目布局和春季测试支持问题

[英]project layout and spring-test support issues

We have project layout as below. 我们的项目布局如下。

src src

src/test/java src /测试/ java

src/test/resources src / test / resources

and we cant add src/main/(java, resources) for code, because of earlier developemnt done. 由于早期开发已经完成,因此我们无法为代码添加src / main /(java,resources)。

src/test/java is having both unit and integration tests in same package as class under test has in src folder. src / test / java与src文件夹中的被测类在同一个程序包中包含单元测试和集成测试。

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. 当测试类和配置文件与被测类一起位于src文件夹中且与类相同的程序包时,集成测试运行得很好。

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. 但是当我将测试类放在src / test / java中并将配置文件放在src / test / resources中时,由于上下文初始化问题,测试无法运行。

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. 1为所有src构建输出文件夹,src / test / java和src / test / resources仅是src文件夹。

2 i am using classpath*: to specify config location, as otherwise spring fails to locate config file in resource folder. 2我正在使用classpath *:指定配置位置,否则spring无法在资源文件夹中找到配置文件。

@ContextConfiguration(locations={"classpath*:applicationContext_getCorpAcctPrefDetailsSP.xml"})

3 tried both @Autowired and setter based DI for test classes 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.

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. 当集成测试位于被测类旁边的src文件夹中时,一切工作正常。
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. 我怀疑当spring作为被测类创建上下文不在与test相同的源文件夹中时,differect源文件夹(src和test)创建问题。

thanks nBhati 谢谢nBhati

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. 在运行时,Spring不在乎(或不知道)原始源代码所在的文件夹。重要的是类路径-哪些编译文件和哪些资源文件夹放在类路径中。 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. 如果您遇到关于在运行测试时找不到的XML文件的错误,则强烈建议在运行测试时这些XML文件不在类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM