简体   繁体   English

IntelliJ在类路径上包含来自另一个Maven模块的测试类

[英]IntelliJ includes test-classes from another maven module on classpath

I have a problem accessing resource file, when I run tests in IntelliJ. 在IntelliJ中运行测试时,访问资源文件时遇到问题。 When I run the same tests from maven everything works as expected. 当我从Maven运行相同的测试时,一切都会按预期进行。

The maven3 project has 2 modules: database-module and webapp-module. maven3项目包含2个模块:数据库模块和webapp模块。 webapp-module depends on database-module. webapp-module取决于数据库模块。

Files: 档案:

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

There is a test class DataSourceTest in database-module. 数据库模块中有一个测试类DataSourceTest。 The test uses dataSource.xml from classpath. 该测试使用来自classpath的dataSource.xml。

DataSourceTest.java contains: DataSourceTest.java包含:

@Value("classpath:dataSource.xml")
private Resource myDataSource;

The annotation @Value is org.springframework.beans.factory.annotation.Value. 注释@Value是org.springframework.beans.factory.annotation.Value。

When I run the test, the variable myDataSource contains the data from src/test/resources. 当我运行测试时,变量myDataSource包含来自src / test / resources的数据。 It works well for maven and for IntelliJ. 它适用于Maven和IntelliJ。

I have another test HomeControllerTest in webapp-module. 我在webapp模块中有另一个测试HomeControllerTest。 The test also uses dataSource.xml in the same way as DataSourceTest. 该测试还以与DataSourceTest相同的方式使用dataSource.xml。 However there is a difference in running the test from maven and from IntelliJ: 但是,从maven和IntelliJ运行测试存在差异:

If I run HomeControllerTest from maven, it works as expected: it uses database-module/src/main/resources/dataSource.xml. 如果我从maven运行HomeControllerTest,它会按预期工作:它使用database-module / src / main / resources / dataSource.xml。 If I run HomeControllerTest from IntelliJ, it uses database-module/src/test/resources/dataSource.xml. 如果我从IntelliJ运行HomeControllerTest,它将使用database-module / src / test / resources / dataSource.xml。

The file dataSource.xml is present only in database-module, there is no dataSource.xml in webapp-module. 文件dataSource.xml仅存在于数据库模块中,而webapp模块中没有dataSource.xml。

I printed out the classpath in IntelliJ while running HomeControllerTest and I was surprized that test-classes from database-module is present, even though HomeControllerTest is part of webapp-module. 我在运行HomeControllerTest时在IntelliJ中打印了类路径,但我感到惊讶的是,即使HomeControllerTest是webapp-module的一部分,也存在来自数据库模块的测试类。

The classpath (when the test is run from IntelliJ): 类路径(从IntelliJ运行测试时):

C:/myproject/database-module/test-classes
C:/myproject/database-module/classes
C:/myproject/webapp-module/test-classes
C:/myproject/webapp-module/classes

The tests use JUnit4. 测试使用JUnit4。

Why is there database-module/test-classes on classpath when I invoke the test from webapp-module? 当我从webapp-module调用测试时,为什么在classpath上存在数据库模块/测试类? How can I change IntelliJ behaviour to match the maven way of constructing classpath? 如何更改IntelliJ行为以匹配构造类路径的行家方式?

The problem turned out to be an issue with IntelliJ version 13. I tested the described problem and my test works when I run only one particular test. 该问题原来是IntelliJ 13版本的问题。我仅测试了一个特定测试时就测试了所描述的问题,并且测试工作正常。 However when I try to run all the test ("Test kind": "All in package" and "Search for tests": "In whole project") I get into the trouble described in the question. 但是,当我尝试运行所有测试时(“测试种类”:“全包”和“搜索测试”:“整个项目”),我遇到了问题中所述的麻烦。

According to JetBrains JIRA the issue is fixed in IntelliJ version 14 - I didn't check. 根据JetBrains JIRA的说法,该问题已在IntelliJ 14版中修复-我没有检查。

https://youtrack.jetbrains.com/issue/IDEA-123962 https://youtrack.jetbrains.com/issue/IDEA-123962

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

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