当我在IntelliJ中运行测试时,访问资源文件时遇到问题。当我从Maven运行相同的测试时,一切都按预期工作。IntelliJ包含类路径上的另一个maven模块的测试类
maven3项目有2个模块:数据库模块和webapp模块。 webapp-module依赖于数据库模块。
文件:
database-module/src/main/resources/dataSource.xml
database-module/src/test/resources/dataSource.xml
database-module/src/test/java/org/example/DataSourceTest.java
webapp-module/src/test/java/org/example/HomeControllerTest.java
有数据库模块测试类DataSourceTest。该测试使用classpath中的dataSource.xml。
DataSourceTest.java包含:
@Value("classpath:dataSource.xml")
private Resource myDataSource;
注释@Value是org.springframework.beans.factory.annotation.Value。
当我运行测试时,变量myDataSource包含来自src/test/resources的数据。它适用于Maven和IntelliJ。
我在webapp-module中有另一个测试HomeControllerTest。该测试还以与DataSourceTest相同的方式使用dataSource.xml。然而,从Maven和IntelliJ运行测试有所不同:
如果我从Maven运行HomeControllerTest,它将按预期工作:它使用database-module/src/main/resources/dataSource.xml。 如果我从IntelliJ运行HomeControllerTest,它使用database-module/src/test/resources/dataSource.xml。
文件dataSource.xml仅存在于数据库模块中,webapp-module中没有dataSource.xml。
我在运行HomeControllerTest时在IntelliJ中输出了类路径,而且我惊讶地发现数据库模块中的测试类存在,即使HomeControllerTest是webapp模块的一部分。
类路径(当测试从运行的IntelliJ):
C:/myproject/database-module/test-classes
C:/myproject/database-module/classes
C:/myproject/webapp-module/test-classes
C:/myproject/webapp-module/classes
该测试使用JUnit4。
当我从webapp-module调用测试时,为什么classpath上有数据库模块/测试类? 如何更改IntelliJ行为以匹配构建类路径的maven方式?
你有没有从你的web应用模块依赖于你的数据库模块? – 2014-12-04 17:09:43
是的,我喜欢。当我执行“mvn clean install”时,首先构建数据库模块,然后是webapp模块。 HomeControllerTest使用数据库模块中的dataSource.xml,因此显然存在依赖关系。但是,当我使用IntelliJ时,HomeControllerTest将拾取错误的dataSource.xml(即。database-module/src/test/resources/dataSource.xml,而不是database-module/src/main/resources/dataSource.xml)。 – tafit3 2014-12-04 18:47:57
您确定数据库模块测试jar中的webapp-module中没有依赖项吗? 什么是Intellij运行/调试配置 - >您的测试 - >使用模块的类路径 – 2014-12-05 10:16:41