Howto add another test source folder to Maven and compile it to a separate folder?
Asked Answered
N

4

50

I have the default src/test/java folder for our unit tests. A separate folder src/integration/java is available for the integration tests.

I configured the maven-surefire-plugin to execute the unit/integration tests in their respective phases. This works great when the compiled classes are in the correct directory. Unfortunately Maven only supports one test source folder and one test output folder.

With mavens build-helper plugin I could add another test-source folder but the compiled classes will be generated into test-classes but I want to compile the classes from src/integration/java into target/integration-test-classes. Is this possible?

src/test/java > target/test-classes
src/integration/java > target/integration-test-classes

PS: I don't like this exclude/include on package base solution (exclude all **/it/** files from the default test phase, and exclude all **/unit/** from the integration phase.

Norean answered 13/4, 2012 at 9:42 Comment(1)
Move your integration tests into a separate maven module.Fluoresce
P
13

Sorry, there's no way of doing that, even when considering some hacking. The concept here is there's only one target directory for compiled classes and one for compiled test classes (even the single <build> tag exposes this). To be honest, I don't really think it should be possible with Maven. Maven promotes straight, clean and legible design of your application, by using well-crafted modules.

I think that what you really want to do is actually to create the integration tests module. That's the common practice, by the way. So far I've always had separate integration testing module and never have had any problems with it. You should depend on all modules needed to run these tests. You can even depend on other module's test classes by using <type>test-jar</type> within your dependency declaration, as mentioned here:

http://maven.apache.org/guides/mini/guide-attached-tests.html

I don't like this method, however, and usually prefer to have separate module with testing support stuff, like base classes for JUnit test cases etc.

Ploughboy answered 13/4, 2012 at 10:4 Comment(1)
github.com/khmarbaise/maui/issues/2 I think this old gihub issue explains how to achieve what topic starter wants.Toland
W
53

Based what you've written it sounds like you didn't named your integration tests correctly and you didn't use the maven-failsafe-plugin for your integration tests. Based on the convention of the maven-failsafe-plugin you should name your integration tests like *IT.java. If you named your integration tests appropriately you can handle that with a more or less configuration like this:

<project ...>
  [...]
  <build>
    [...]
     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
          <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/integration/java</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      [...]
  </build>
  [...]
</project>

With the above it's possible to hold the integration tests within the same module. But this will not solve the idea of having the compiled integration tests classes into a separate folder.

Sometimes it's better to have a separate integration test module which contains only the integration tests (which results in having a multi-module build). If you like to leave the conventions of Maven you can try to configure the maven-compiler-plugin to use a different output path (eg. target/integration-tests/classes) which don't think will really work.

Whereto answered 13/4, 2012 at 18:1 Comment(1)
This worked for me but only if I set it to <phase>validate</phase>.Nave
F
19

If you only want to change the unit test source folder (rather than add an additional one), just change the testSourceDirectory element:

<build>
    <testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>

This is useful if all of your unit tests are written in groovy. (But you will also need to configure maven to compile your groovy code too - see the groovy-eclipse-maven-plugin or the build-helper-maven-plugin.)

Field answered 1/12, 2016 at 18:0 Comment(0)
P
13

Sorry, there's no way of doing that, even when considering some hacking. The concept here is there's only one target directory for compiled classes and one for compiled test classes (even the single <build> tag exposes this). To be honest, I don't really think it should be possible with Maven. Maven promotes straight, clean and legible design of your application, by using well-crafted modules.

I think that what you really want to do is actually to create the integration tests module. That's the common practice, by the way. So far I've always had separate integration testing module and never have had any problems with it. You should depend on all modules needed to run these tests. You can even depend on other module's test classes by using <type>test-jar</type> within your dependency declaration, as mentioned here:

http://maven.apache.org/guides/mini/guide-attached-tests.html

I don't like this method, however, and usually prefer to have separate module with testing support stuff, like base classes for JUnit test cases etc.

Ploughboy answered 13/4, 2012 at 10:4 Comment(1)
github.com/khmarbaise/maui/issues/2 I think this old gihub issue explains how to achieve what topic starter wants.Toland
D
0

I was not able to create multiple test source files and get the code to run in Eclipse. I do understand that there are many reasons why one might want multiple test source folders including the following

  • Tests should be in the same package as the code that is being tests
  • There are multiple types of automated tests that should be included in any non trivial application including unit tests, integration tests, tests of external resources, services, libraries, etc.
  • We often inherit very long running code bases with a mixture of good and bad/dangerous tests that need to be cleaned up. It would be nice if we could keep these tests around while we cull through them and build up our new automated testing base whilst still being able to run either the old tests or the new tests separately (both as individual tests and the ability to run all of the tests in each category at once)
  • Tests of different types need to be run at different times (e.g. code check in, nightly builds, long running tests, etc.) depending on the resources required to run the tests. Managing this through naming convention gets very cluttery in large scale projects
  • Tests need to be separate from production code (i.e. we don't want test classes deployed as part of the production release).
  • Tests often required additional code to automate processes that are not really part of what should be deployed to production (e.g. classes that implement non trivial process to get resources such as building a complex data structure from data from a service or database)

My solution for this was to create a new project and then create multiple source folders (not test source folders) in the new project based upon what is written here: https://www.baeldung.com/maven-project-multiple-src-directories.

This satisfies all of the requirements listed above.

I eagerly await your dings but please do let us know the down side to this. Prove me wrong ;)

Demonstrate answered 14/4, 2021 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.