Spring在第一次失败时停止测试

Spring在第一次失败时停止测试

本文介绍了使用Maven/JUnit/Spring在第一次失败时停止测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Maven在遇到第一个错误时停止尝试运行JUnit Spring测试.这可能吗?

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

我的测试类如下所示,我将它们作为标准的Maven目标运行.

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
{
...

如果Spring配置中存在错误,则每个测试将尝试重新启动Spring上下文,这需要20秒钟的时间.这意味着我们永远不会发现任何测试都失败了,因为它将在断定构建失败之前尝试全部运行!

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!

推荐答案

这不仅仅是一个评论,而不是一个答案,但是,也许您会发现它很有用.

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

我建议将集成测试分为一个单独的阶段,并使用故障保护而不是Surefire来运行它们.这样,您可以决定是否只需要运行快速的单元测试,还是需要运行长时间的集成测试的完整套件:

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>

解决问题的方法可能是将测试分为一个单独的执行,然后首先运行它;这样,执行将失败,并且随后的surefire/failsafe执行将不会启动.请参见如何配置插件来实现.

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.

这篇关于使用Maven/JUnit/Spring在第一次失败时停止测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:10