【问题标题】:How do I make src/test/resources files available to other projects' unit tests?如何使 src/test/resources 文件可用于其他项目的单元测试?
【发布时间】:2016-11-11 11:09:07
【问题描述】:
例如,有一个项目 my-common 包含 src/main/resources 中的 my.properties 文件和另一个版本src/test/resources,用于测试。
还有其他项目,例如my-service,它使用my-common和src/main/resources/my.properties em> 值。
如何使 src/test/resources/my.properties 可用于 my-services/src/test/java 中的单元测试?
【问题讨论】:
标签:
java
maven
testing
resources
【解决方案1】:
标准的 Maven 方法是 create a test jar。 maven插件maven-jar-plugin与目标test-jar一起使用:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
然后,您需要依赖模块中的测试 jar:
<project>
...
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>