In maven, how do you run separate configurations for different junit test classes

923 Views Asked by At

For a list of classes (which I can specify as a list of includes), I want to run with the forkMode=always because they mess with static state (legacy code).

For the remaining classes (which I can specify as excluding the classes above), I want to run with the forkMode=never, or some other configuration.

The motivation is that tests were taking forever to run with forkMode=always, because the classloader keeps having to reload everything, just to run tests from one test class!

Some other details: - all tests classes must run within the same profile (ie: -P release)

Is it possible to just have multiple :

<plugin>
   <artifactId>maven-surefire-plugin</artifactId>

within the same profile?

1

There are 1 best solutions below

2
On BEST ANSWER

One way I could think of achieving this is defining multiple executions:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <executions>
        <execution>
            <id>legacy-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/RemainingClasses*.java</exclude>
                </excludes>
                <includes>
                    <include>**/LegacyClasses*.java</include>
                </includes>
                <forkMode>always</forkMode>
            </configuration>
        </execution>
        <execution>
             <id>other-tests</id>
             <phase>test</phase>
             <goals>
                 <goal>test</goal>
             </goals>
             <configuration>
                 <includes>
                     <include>**/RemainingClasses*.java</include>
                 </includes>
                 <excludes>
                     <exclude>**/LegacyClasses*.java</exclude>
                 </excludes>
                 <forkMode>never</forkMode>
             </configuration>
        </execution>
   </executions>
</plugin>

On the other side, you might want to migrate to using reuseForks and forkCount instead.