16

I want to use Junit 5 in Maven project:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

I want currently to disable the test:

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;

@Disabled
public class DatabaseFeaturesBitStringTest {
    .... 
}

But it's not working. Tests are executed after mvn clean build. Can you advise what I'm missing?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Can you tell if the result says no tests were skipped? It should be working as desired but maybe it looks like the disabled test was executed. When I run a test class containing one disabled of **n** test cases, the result looks like **Runs: n/n (1 skipped)** – deHaar Aug 28 '18 at 09:41
  • Tested with maven 3.5.3 and junit-jupiter-engine 5.0.1, works fine, test is skipped. – staszko032 Aug 28 '18 at 10:15
  • @staszko032 can you please paste your POM configuration? Probably I'm missing something. – Peter Penzov Aug 28 '18 at 10:36
  • Before that, could you check if you configure maven-surefire-plugin in POM? – staszko032 Aug 28 '18 at 10:42
  • Yes - I was missing maven-surefire-plugin in POM. Thanks! – Peter Penzov Aug 28 '18 at 10:47

5 Answers5

14

This is caused by incompatibilities betweeen maven-surefire-plugin and junit5. Your either have to define a version with at least 2.22.0 for the maven-surefire-plugin (see codefx blog - junit 5 setup) or just use maven 3.6.0. Additionally you have to have the dependency to the jupiter-engine defined as already stated in the very first lines of this question above:

<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <version>5.4.0</version>
   <scope>test</scope>
</dependency>

If you defined a dependency to the artifact junit-jupiter-api only, which is sufficient to get the test compiled and run with junit5, the @Disabled annotation will be silently ignored and the particular test will get run as well.

Jens Vagts
  • 595
  • 4
  • 7
6

Check your configuration of surefire plugin for junit-jupiter-engine dependency. I am not sure, but I believe it should be configured in order to load all features from engine artifact, including Disabled annotation.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.surefire.provider.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.version}</version>
                </dependency>
            </dependencies>
        </plugin>
staszko032
  • 802
  • 6
  • 16
  • 1
    Are you sure for the dependencies? Without them it's running also. – Peter Penzov Aug 28 '18 at 11:01
  • I took it from https://junit.org/junit5/docs/current/user-guide/#running-tests-build, without such dependencies my JUnit 5 based tests are not called by maven, maybe it is version dependent. – staszko032 Aug 28 '18 at 11:10
  • surefire provider is not needed anymore, https://github.com/junit-team/junit5/issues/1536 – rmuller Jun 09 '19 at 09:21
  • 1
    @rmuller is right. The Surefire and Failsafe warns about using an external JUnit5 provider. These plugins already have their own providers. Please see this tutorial http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html – tibor17 May 02 '20 at 10:22
1

@staszko032 The xml code in POM you described is the era of 2018. In 2019 we adopted JUnit5 provider to Apache project and the code https://stackoverflow.com/a/52056043/2758738 is no more legal.

@Jens Vagts There are no incompatibilities between JUnit5 and Surefire/Failsafe because Apache plugin uses JUnit5 platform launcher which does all this trick.Surefire and Failsafe plugin does not execute your tests. So there is nothing to be incompatible, since all the work is done by JUnit5 engines and not plugin's code. It is the real engine defined in your POM which executes the tests. The plugin only triggers the engine which does all the work and sends the events back to plugin.

Please see Apache documentation:

https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/junit-platform.html

tibor17
  • 1,043
  • 6
  • 9
1

Maybe check if Jupiter conditions (@Disabled in your case) are deactivated via configuration parameter junit.jupiter.conditions.deactivate.

Please see JUnit5 documentation for more details: https://junit.org/junit5/docs/current/user-guide/#extensions-conditions-deactivation

lu_ko
  • 4,055
  • 2
  • 26
  • 31
0

this might be problem with improper imports. i solved it by removing all junit import statements and re-imported all junit classes properly.