Search code examples
javamavenmaven-surefire-pluginmaven-compiler-pluginbuild-helper-maven-plugin

COMPILATION ERROR : package my.package.root.util does not exist


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

Solution

  • 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>