如果@SpringBootTest无法加载ApplicationContext,如何使failsafe-plugin失败

问题描述

我的@SpringBoottest由maven-failsafe-plugin执行。只要可以加载ApplicationContext,就可以正常工作。 如果无法加载ApplicationContext(例如,由于测试中缺少bean),Spring将显示类似的内容

2020-11-04 12:01:41 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION Failed TO START
***************************

Description:
...

2020-11-04 12:01:41 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3e61138c] to prepare test instance [foo.bar.someControllerSecurityIT@4af9cb68]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)

并且测试根本没有计算

这是认值吗?有没有办法将这种情况视为失败?

junit测试报告正在显示

-------------------------------------------------------------------------------
Test set: foo.bar.someControllerSecurityIT
-------------------------------------------------------------------------------
Tests run: 0,Failures: 0,Errors: 0,Skipped: 0,Time elapsed: 5.636 s - in foo.bar.someControllerSecurityIT

即使测试类包含六个方法,这些方法解决了原因(缺少bean)之后仍在绿色运行。

也许值得一提的是,这六个方法是返回@TestFactory的JUnit Stream<DynamicTest>方法

编辑:在这种情况下,这似乎是问题所在。实现“正常” @Test方法会导致对该测试进行计数,并且由于缺少/失败的初始化(?)导致该测试类失败并出现1个错误。这会导致整个构建失败。


要复制的源(如我上面所述,与Spring Boot无关)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foo.bar</groupId>
    <artifactId>failing-junit-extension</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

FailingJunitExtension.java

package foo.bar;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class FailingJunitExtension
    implements
        BeforeAllCallback
{
    public void beforeAll( ExtensionContext context )
        throws Exception
    {
        System.err.println( "Extension thrown in beforeAll" );
        throw new RuntimeException();
    }
}

FailingExtensionIT.java

package foo.bar;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(FailingJunitExtension.class)
class FailingExtensionIT
{
    @TestFactory
    Stream<DynamicTest> dummyTestFactory()
    {
        return IntStream.iterate( 0,n -> n + 2 ).limit( 10 )
                .mapToObj( n -> DynamicTest.dynamicTest( "test" + n,() -> assertTrue( n % 2 == 0 ) ) );
    }
}

呼叫mvn clean verify会导致

[INFO] Running foo.bar.FailingExtensionIT
Extension thrown in beforeAll
[INFO] Tests run: 0,Time elapsed: 0.016 s - in foo.bar.FailingExtensionIT
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0,Skipped: 0

根本没有注册任何测试。

这是junit还是故障安全插件的问题?

解决方法

似乎此问题已在failsafe-plugin 3.0.0-M5中解决。