'How using parameter "-Dsurefire.skipAfterFailureCount=1" to skip tests after the first crash

I have a Maven test project. In which I use context-related tests. I use maven-surefire-plugin to run tests. I need to make the test stop running after the first failed test. I found a way to do this through -Dsurefire.skipAfterFailureCount=1, but it doesn't work. Maybe I'm doing something wrong? Below is my pom.xml:

    <properties>
        <selenide.version>5.3.0</selenide.version>
        <junit.jupiter.version>5.5.1</junit.jupiter.version>
        <selenium.java.version>3.141.59</selenium.java.version>
        <allure.junit5.version>2.12.1</allure.junit5.version>
        <aspectj.version>1.8.10</aspectj.version>
        <maven.surefire.plugin.version>3.0.0-M3</maven.surefire.plugin.version>
        <junit.platform.launcher>1.5.2</junit.platform.launcher>
        <junit.jupiter.engine>5.5.2</junit.jupiter.engine>
        <junit.vintage.engine>5.5.2</junit.vintage.engine>
        <allure.maven.version>2.10.0</allure.maven.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>

                <configuration>

                    <argLine>-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    -Dsurefire.skipAfterFailureCount=1
                    </argLine>

                    <systemProperties>

                        <property>

                            <name>allure.results.directory</name>
                            <value>${project.build.directory}/allure-results</value>
                        </property>

                    </systemProperties>

                </configuration>

            </plugin>
        </plugins>
    </build>

Also I tried this option:

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>

                <configuration>
                    <skipAfterFailureCount>1</skipAfterFailureCount>
                    <argLine>-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"</argLine>

                    <systemProperties>

                        <property>

                            <name>allure.results.directory</name>
                            <value>${project.build.directory}/allure-results</value>
                        </property>

                    </systemProperties>

                </configuration>

            </plugin>
        </plugins>
    </build>

Does anyone know how to solve this problem? Why is my code not working? Did I make a mistake somewhere?



Solution 1:[1]

This works in jUnit 4. I don't know why your code isn't working, but maybe this will help.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
                <skipAfterFailureCount>1</skipAfterFailureCount>
            </configuration>
        </plugin>
    </plugins>
</build>

Solution 2:[2]

  1. add the plugin.
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.7.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <type>maven-plugin</type>
  </dependency>
</dependencies>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
      <skipAfterFailureCount>1</skipAfterFailureCount> 
    </configuration>
  </plugin>
</plugins>
  1. don't use @TestMethodOrder in your code.
import java.util.HashMap;

import jp.co.nisshinsci.saas.framework.dto.Member;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

// @TestMethodOrder(MethodOrderer.DisplayName.class)
public class test {
    @Test
    void test01() throws Exception {
        HashMap stringHashMap = new HashMap();
        long xxx = ((Member) stringHashMap.get("xxx")).photoVersion;
    }

    @Test
    void test02() throws Exception {
        HashMap stringHashMap = new HashMap();
        long xxx = ((Member) stringHashMap.get("xxx")).photoVersion;
    }

    @Test
    void test03() throws Exception {
        HashMap stringHashMap = new HashMap();
        long xxx = ((Member) stringHashMap.get("xxx")).photoVersion;
    }
}

  1. test by maven, not by Intellij Idea.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohnP2
Solution 2 kyakya