Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JUnit tests by category in Maven?

People also ask

How do you run a JUnit test case in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do I run a specific test in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

How do I run a JUnit test case using Maven in Eclipse?

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.


Maven has since been updated and can use categories.

An example from the Surefire documentation:

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
        <groups>com.mycompany.SlowTests</groups>
      </configuration>
</plugin>

This will run any class with the annotation @Category(com.mycompany.SlowTests.class)


Based on this blog post - and simplifying - add this to your pom.xml:

<profiles>
    <profile>
        <id>SlowTests</id>
        <properties>
            <testcase.groups>com.example.SlowTests</testcase.groups>
        </properties>
    </profile>
    <profile>
        <id>FastTests</id>
        <properties>
            <testcase.groups>com.example.FastTests</testcase.groups>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.13</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.13</version>
                </dependency>
            </dependencies>
            <configuration>
                <groups>${testcase.groups}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

then at the command line

mvn install -P SlowTests
mvn install -P FastTests
mvn install -P FastTests,SlowTests

I had a similar case where I want to run all test EXCEPT a given category (for instance, because I have hundreds of legacy uncategorized tests, and I can't / don't want to modify each of them)

The maven surefire plugin allows to exclude categories, for instance:

<profiles>
    <profile>
        <id>NonSlowTests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <excludedGroups>my.category.SlowTest</excludedGroups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

You can use

mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests"

But ensure that you configure properly the maven surefire plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.11</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>2.12.2</version>
    </dependency>
  </dependencies>
</plugin>

See docs in: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html


I lost lot of time on this error "groups/excludedGroups require TestNG or JUnit48+ on project test classpath" because I thought I was using a bad version of junit, or a bad version of the surefire plugin, or a combination that does not fit.

It was none of that: in my project I had a "config" module that was built before the module I wanted to test. This module had no junit dependency -> it had no junit on the classpath...

This mistake may help others...


Not exactly the same thing but using surefire plugin, test classes can be chosen based on file name. You are not using Junit Categories though.

An example for running just DAO tests.

<executions>
  <execution>
     <id>test-dao</id>
        <phase>test</phase>
          <goals>
             <goal>test</goal>
        </goals>
          <configuration>
             <excludes>
                <exclude>none</exclude>
            </excludes>
            <includes>                  
                <include>**/com/proy/core/dao/**/*Test.java</include>
            </includes>
        </configuration>
  </execution>

http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html


For the use-case where you want to ignore certain tests by default and then conditionally run all tests from the command line, this setup will work on JUnit 4.9.

First create the marker interface:

public interface IntegrationTest {}

Annotate tests to be excluded:

@Category(IntegrationTest.class)
public class MyIntegrationTest() {}

Add plugins and profiles to pom.xml:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <!-- Read JUnit categories to be excluded from Maven property -->
    <configuration>
      <excludedGroups>${test.excluded.groups}</excludedGroups>
    </configuration>
  </plugin>
</plugins>
  
<profiles>
  <profile>
    <id>Default</id>
    <!-- Set profile to be active by default -->
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <test.excluded.groups>com.example.IntegrationTest</test.excluded.groups>
    </properties>
  </profile>
  <profile>
    <id>AllTest</id>
    <!-- Set groups property to blank value which will match nothing -->
    <properties>
      <test.excluded.groups></test.excluded.groups>
    </properties>
  </profile>
</profiles>

When running tests as usual from command line, integration test will be excluded:

mvn test

All tests including integration tests can be activated with the corresponding profile:

mvn test -P AllTest