Stop Tests on first Failure with Maven/JUnit/Spring
Asked Answered
T

2

5

I'd like Maven to stop trying to run my JUnit Spring tests when it encounters the first error. Is this possible?

My test classes look like the following, and I run them just as a standard Maven target.

@ContextConfiguration(locations = {"classpath:/spring-config/store-persistence.xml","classpath:/spring-config/store-security.xml","classpath:/spring-config/store-service.xml", "classpath:/spring-config/store-servlet.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class SkuLicenceServiceIntegrationTest
{
...

If there's an error in the Spring config, then each test will try to restart the Spring context, which takes 20 seconds a go. This means we don't find out for ages that any tests have failed, as it'll try to run the whole lot before concluding that the build was a failure!

Thievery answered 17/10, 2011 at 10:38 Comment(2)
BTW: #1924357Sirloin
Does this answer your question? Is there a way to "fail fast" for junit with the maven surefire plugin?Osithe
D
3

Answering ten years later, since I needed exactly the same.

The failsafe plugin can be configure to "skip tests after failure", using the skipAfterFailureCount parameter.

Official documentation:

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/skip-after-failure.html

Drought answered 17/3, 2022 at 3:1 Comment(0)
S
1

This is more a remark, than an answer, but still, maybe you'll find it useful.

I'd recommend separating your integration tests into a separate phase, and running them with Failsafe, rather than Surefire. This way you can decide whether you need to run only fast unit tests, or the complete set with long-running integration tests:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <!-- Uncomment/comment this in order to fail the build if any integration test fail -->
                <execution>
                    <id>verify</id>
                    <goals><goal>verify</goal></goals>  
                </execution>
            </executions>
        </plugin>
    </plugins>

A workaround for your problem might be singling out a test into a separate execution and run it first; this way the execution would fail and subsequent surefire/failsafe executions will not be launched. See how to configure the plugin to do it.

Sirloin answered 17/10, 2011 at 10:51 Comment(1)
At least with Maven 2.2.1, it seems that with separate executions of failsafe, a failure of the first execution doesn't prevent the second execution from running anyway.Janeljanela

© 2022 - 2024 — McMap. All rights reserved.