1

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
Frederic
  • 2,015
  • 4
  • 20
  • 37
  • Is there `package my.package.root.util;` added at the begining of the `StringUtilsTests.java` file? – JockX Nov 18 '21 at 22:00
  • It may be because your test root is in your source root. I assume you set `src`, right? – Oliver Nov 18 '21 at 22:04
  • @JockX No because the file is in src/test/java. Therefore I have this instead `package test.java;` – Frederic Nov 18 '21 at 23:19
  • Your package should be empty. `test.java` cannot be considered part of the package name, as the java folder is compilation root. – JockX Nov 18 '21 at 23:21
  • @Oliver No, i didn't add ``. Where do you think I should add that? – Frederic Nov 18 '21 at 23:21
  • Goes under ``. – Oliver Nov 18 '21 at 23:53
  • @JockX are you sure ? When I look at examples, package is provided. See [here](https://github.com/eugenp/tutorials/blob/eda9dd0b7e87dbdb46c5c55e788c0cbc87124f2c/maven-modules/maven-surefire-plugin/src/test/java/com/baeldung/runasingletest/TheFirstUnitTest.java#L1) – Frederic Nov 19 '21 at 00:36
  • @Oliver did you mean `testSourceDirectory` instead of `sourceDirectory` ? – Frederic Nov 19 '21 at 03:51
  • 1
    You should follow the conventions and put your productive code into `src/main/java/` and your unit tests into `src/test/java/` also follow the naming convention for tests. Unit Tests like `*Test.java` and integration tests like `*IT.java` (via maven-failsafe-plugin)...Than no build helper plugin is needed... – khmarbaise Nov 19 '21 at 08:15
  • @khmarbaise Like I said, I don't want to change my structure. Like [this article](https://resheim.net/2014/12/unit-testing-eclipse-rcp-applications.html) says well "This layout is not commonly found in Eclipse bundles" – Frederic Nov 19 '21 at 16:43
  • The article is related to an OSGi project which is different... If your project is an OSGi project you should use [maven tycho](https://www.eclipse.org/tycho/sitedocs/) – khmarbaise Nov 19 '21 at 18:42

2 Answers2

2

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>

zeg
  • 432
  • 2
  • 9
  • 1
    I initially tried with a wildcard but not only it didn't work, I saw [that](https://stackoverflow.com/a/35514742/7066681) where it is mentioned that it is not supported. As for running them as integration tests, I am not sure to understand what would be the difference ? – Frederic Nov 19 '21 at 00:56
  • The maven build lifecycle is 1 validate 2 compile 3 test 4 package 5 verify 6 install 7 deploy see here: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 1) basicly your compiler validated that there are no underlined errors, 2) your .java files are now .class files and 3) you run Unit-Tests on them. After 4) package your local source gets mapped and packaged with the other referenced project. now you can run it and it has the non local dependencies if you like to say so. 5) on that ready to use bundle integration tests are performed. – zeg Nov 19 '21 at 01:01
  • For example if you are programmin a webserver a normal unit test (...Test.java) could check if your function transforms a string correctly to a result. an integration test (...IT.java) could run the webserver, create a http client and send probes to it. (And a systemintegrationtest is runned if you connect multiple programs to a whole system. this is normally done by hand or by automated tests on a dedicated testenvironment outside of your programming IDE.) – zeg Nov 19 '21 at 01:06
  • I have changed the phase to `integration-test` but it still fails on the testCompile phase. – Frederic Nov 19 '21 at 16:42
2

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>
Frederic
  • 2,015
  • 4
  • 20
  • 37