Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果@SpringBootTest无法加载ApplicationContext,如何使故障保护插件失败_Java_Spring Boot_Junit5_Maven Failsafe Plugin - Fatal编程技术网

Java 如果@SpringBootTest无法加载ApplicationContext,如何使故障保护插件失败

Java 如果@SpringBootTest无法加载ApplicationContext,如何使故障保护插件失败,java,spring-boot,junit5,maven-failsafe-plugin,Java,Spring Boot,Junit5,Maven Failsafe Plugin,我的@SpringBootTest由maven failsafe插件执行。只要可以加载ApplicationContext,就可以正常工作。 如果无法加载ApplicationContext(例如,由于测试中缺少一个bean),Spring将显示如下内容 2020-11-04 12:01:41 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED

我的
@SpringBootTest
由maven failsafe插件执行。只要可以加载
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
方法,返回一个

编辑:看来这就是那种情况下的问题。实现“normal”
@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>
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 ) ) );
    }
}
根本没有注册任何测试


这是junit还是failsafe插件的问题?

这个问题似乎在failsafe插件3.0.0-M5中得到了解决。

请为您的测试和故障保护配置添加代码。我已将其添加到初始帖子的末尾。
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 ) ) );
    }
}
[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