I have an Eclipse Java project for which I am trying to execute the unit tests using Maven.
I have my unit tests as below so that it respects the expected hierarchy
src/test/java/StringUtilsTests.java
However, my unit test references the source code located in:
src/my/package/root/util/StringUtils.java
just because it has always been like this and I don't want to change my folder hierarchy for the tests.
Therefore, I use the build-helper-maven-plugin to add this source folder, as below
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/my/package/root/util</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
But even with that, I get the below error:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/frederic/git/myproject/src/test/java/StringUtilsTests.java:[22,32] package package my.package.root.util does not exist
Below is my reference to the maven-surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
And it's even worse if I don't put my test in
src/test/java/StringUtilsTests.java
but instead here for example:
src/tests/my/package/root/util/StringUtilsTests.java
The maven-surefire-plugin will show
No tests to run
CodePudding user response:
Do you need to run them as Unit-Tests or can you run them as Integration-Tests? I think Integration tests would work out-of-the-box like this? Simply change the class names to end with IT MyClassIT.java
for these tests or finetune your .pom and change the goal to integration-test-phase or verification-phase? I think that should work since it will be executed later on in the build cycle.
Otherwise try to change this line with a wildcard:
<source>src/my/package/root/util/*</source>
CodePudding user response:
Based on this article, adding the below elements to my pom.xml
fixed my issue:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>compiletests</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<testSourceDirectory>${project.basedir}/src-test</testSourceDirectory>
And VERY important was to add this dependency
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>