Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Surefire with different file sets

I have two kinds of Unit tests (not integration test). Because of some strange behaviour with Spring Security I need to run first all normal tests, and later on the security tests.

I am using Junit (so I can not use any TestNG groups).

So what I have done is specify two sets of includes and excludes rules.

<excludes>
   <exclude>**/*SecurityTest.java</exclude>                  
</excludes>
<includes>
   <include>**/*Test.java</include>
   <include>**/*Tests.java</include>
</includes>

and

<excludes>
</excludes>
<includes>
    <include>**/*SecurityTest.java</include>
</includes>

That works if I replace them in my pom by hand so I can have normal or security tests. But of course I want that both kind of tests run in each build.

My first try was to have two complete maven-surefire-plugin configruation. But then maven take only the last of them in account.

My next try was to use two execution definitions, but then surefire seems to ignore the rules at all and run both kind of tests mixed.

So my general question is how to specify two file sets for maven surefire so that they will be executed each after another? And more specific how to specify two different file sets.


the configuration with executions

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <junitArtifactName>junit:junit</junitArtifactName>
        <encoding>UTF-8</encoding>
        <inputEncoding>UTF-8</inputEncoding>
        <outputEncoding>UTF-8</outputEncoding>
        <argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea</argLine>   
    </configuration>

    <executions>
        <execution>
            <id>normal-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/Abstract*.java</exclude>
                    <exclude>**/*_Roo_*</exclude>
                    <exclude>**/*SecurityTest.java</exclude>
                </excludes>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>security-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/Abstract*.java</exclude>
                    <exclude>**/*_Roo_*</exclude>
                </excludes>
                <includes>
                    <include>**/*SecurityTest.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 787
Ralph Avatar asked Aug 25 '11 15:08

Ralph


People also ask

Does surefire run tests in parallel?

The surefire offers a variety of options to execute tests in parallel, allowing you to make best use of the hardware at your disposal. But forking in particular can also help keeping the memory requirements low.

What is forked process in maven?

Pinging forked JVM Simply the mechanism checks the Maven PID is still alive and it is not reused by OS in another application. If Maven process has died, the forked JVM is killed.

Why tests are skipped maven surefire plugin?

When the Surefire plugin reaches the test goal, it will skip the unit tests if the maven. test. skip properties is set to true . Another way to configure Maven to skip unit tests is to add this configuration to your project's pom.

How do I run a specific test case in maven?

If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.


1 Answers

You haven't changed the default surefire execution which is bound to the test phase in the superpom, so it's still running with its default config. The id of that execution is "default-test". You'll either need to override it to unbind it from the test phase or else just use that id for one of your own executions.

like image 59
Ryan Stewart Avatar answered Sep 19 '22 14:09

Ryan Stewart