0

The Maven surefire plugin only runs tests which start or end with the word test. This is really cumbersome to deal with in JUnit for 2 reasons:

1 - I already have to add @Test to all my test methods, so also having to append the word test is repetitive.

2 - In JUnit, if I want to disable some test, I just mark it with @Disabled, so if I am using surefire, I will also have to rename the test method.

Are there any ways to make surefire play nice with JUnit? This way, just running what's marked with @Test and automatically ignoring @Disabled methods?


Currently my pom.xml looks like this (only including the test related items to save space):

<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>

<build>
    <pluginManagement>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
    </pluginManagement>
</build>
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • Your assumption is simply wrong. The surefire plugin is related to the unit test framework you are using...Are you using JUnit 4? How does your pom file look like? – khmarbaise Dec 17 '18 at 17:38
  • I am using JUnit 5 in IntelliJ IDEA, JUnit has `scope` of test. Besides I tested all of this and I read it online that this is the expected behavior, it's not an assumption, I posted it after testing and researching – Michel Feinstein Dec 17 '18 at 17:50
  • `By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns: "**/Test*.java"....` https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html – Michel Feinstein Dec 17 '18 at 17:52
  • Also described here: https://stackoverflow.com/questions/6178583/maven-does-not-find-junit-tests-to-run – Michel Feinstein Dec 17 '18 at 17:57
  • @khmarbaise I will include my pom file parts related to tests in my original post – Michel Feinstein Dec 17 '18 at 18:00
  • 2
    There are two things. You are referencing about the method names and not about the classes in your post...now you are giving references to the naming conventions? Furthermore I strongly recommend to read the docs about JUnit5 support https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html which shows that you are using the wrong dependency (you need to add junit-jupiter-engine and maybe supplemental junit-jupiter-api)...but the engine is the most important one... – khmarbaise Dec 17 '18 at 18:09
  • @khmarbaise thanks this did the trick! – Michel Feinstein Dec 17 '18 at 18:25
  • @khmarbaise you should post your answer as an **answer**. – Sam Brannen Dec 18 '18 at 17:17
  • @SamBranner done as requested. – khmarbaise Dec 18 '18 at 18:44

1 Answers1

1

To run JUnit Jupiter tests with Maven you have to use junit-jupiter-engine instead of junit-jupiter-api.

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

You might add the junit-jupiter-api as a supplemental artifact. Furthermore you should use at least maven-surefire-plugin 2.22.1+

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
khmarbaise
  • 92,914
  • 28
  • 189
  • 235