Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven and JUnit 5: run a single test in a class

Tags:

maven

junit5

I have been trying to run unsuccessfully a single test in one class with Maven (version 3.3.9) and JUnit 5 (NOT 4) with the command:

mvn -Dtest=EmitRulesTest#cr_filter_contact_points_for_C4C_output test

This command executes all tests.

Trying out this command actually executes all tests in the class:

mvn test -Dtest=EmitRulesTest

This is my JUnit 5 Maven configuration:

<dependencies>
    ...
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.0.0-RC2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        ...    
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <systemPropertiesFile>${basedir}/src/test/resources/definitions/system.properties</systemPropertiesFile>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-RC2</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-RC2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Further references: Running a Single Test using Maven

like image 985
gil.fernandes Avatar asked Aug 28 '17 13:08

gil.fernandes


1 Answers

You can use this format:

mvn test -Dtest=TestClass#testMethod

You can find out more information here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

This is an excerpt of my POM.xml

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit.vintage.version}</version>
    <scope>test</scope>
</dependency>

...

<properties>
    <junit.version>4.12</junit.version>
    <junit.jupiter.version>5.1.0</junit.jupiter.version>
    <junit.vintage.version>5.1.0</junit.vintage.version>
    <junit.platform.version>1.1.0</junit.platform.version>
</properties>
like image 170
Ivan Alonso Avatar answered Oct 29 '22 09:10

Ivan Alonso