【问题标题】:How to make failsafe-plugin fail if @SpringBootTest could not load ApplicationContext如果@SpringBootTest 无法加载 ApplicationContext,如何使故障安全插件失败
【发布时间】:2020-11-04 12:49:14
【问题描述】:

我的@SpringBootTests 由 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)后运行绿色的方法。

也许值得一提的是,这六个方法是 JUnit @TestFactory 方法返回一个 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, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 s - in foo.bar.FailingExtensionIT
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

根本没有注册任何测试。

这是 junit 的问题还是故障保护插件的问题?

【问题讨论】:

  • 请为您的测试和故障安全配置添加代码
  • 我已将其添加到初始帖子的末尾。

标签: java spring-boot junit5 maven-failsafe-plugin


【解决方案1】:

看来这个问题在failsafe-plugin 3.0.0-M5中已经解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多