我在IDEA中有一个由maven管理的项目。我在“src”下创建一个用于集成测试的自定义目录,并命名为“intg”:
myproject
|- src
|- intg
|- |- java
|- |- resources
|- main
|- |- java
|- |- resources
|- test
|- java
|- resources
在我的 POM.xml 中,我添加了:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/target/test-classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/intg/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后我在IDEA中重新导入POM.xml,但我发现IDEA没有将“resources”目录视为测试源根目录,所以我手动将其标记为“测试源根”。
然后我将文件“test-data.txt”放入其中,并在我的测试代码中获取它:
@Test
public void shouldGetTheFileFromClasspath() {
URL resource = MyTest.class.getClassLoader().getResource("test-data.txt");
assertNotNull(resource);
}
不幸的是,测试失败了。如果我将“test-data.txt”移动到“src/test/resources”,那么它就会通过。
我不确定哪里出了问题以及如何修复它。折腾了我一整天了,请大家帮忙~
最佳答案
尝试一下:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/intg/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
然后调用“生成资源并更新文件夹”
关于java - 如果项目使用maven,IDEA找不到自定义测试源根目录下的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19697401/