spring-boot - SpringBootTest - 如何断言上下文加载失败

标签 spring-boot integration-testing listener spring-boot-test

我写了一个 ApplicationListener 应该检查环境是否在上下文初始化期间准备好。我在测试场景时遇到了麻烦,因为我在 configure() 和 main() 方法中手动添加了监听器。

应用监听器类:

public class EnvironmentPrepared implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

        @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
            //code that checks if conditions are met

            if (checkTrue) {
            throw new RuntimeException();
        }
    }
}

主要类:
    public class MyApp extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        setRegisterErrorPageFilter(false);
        return application.listeners(new EnvironmentPrepared()).sources(MyApp.class);
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MyApp.class);
        springApplication.addListeners(new EnvironmentPrepared());
        springApplication.run(args);
    }
}

我要执行的测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(loader = OverriddenProfilesTest.CustomLoader.class)
public class OverriddenProfilesTest {

    public static class CustomLoader extends SpringBootContextLoader {

        @Override
        protected SpringApplication getSpringApplication() {
            SpringApplication app = super.getSpringApplication();
            app.addListeners(new EnvironmentPrepared());
            return app;
        }
    }

    /**
     * Checks if spring can bootstrap everything 
     */
    @Test(expected = RuntimeException.class)
    public void test() {

    }
}

这将是我想要的测试。抛出 RuntimeException 但异常发生在上下文初始化期间,因此测试甚至没有开始。

最佳答案

这是我使用的解决方案。我删除了将监听器手动添加到应用程序的操作,而是使用 spring.factories 文件。

关于测试,我首先创建了一个自定义运行器类:

    public class SpringRunnerWithExpectedExceptionRule extends SpringJUnit4ClassRunner {

public SpringRunnerWithExpectedExceptionRule(Class<?> clazz) throws InitializationError {
    super(clazz);
}

@Override
protected Statement methodBlock(FrameworkMethod frameworkMethod) {
    List<ExpectedException> testRules = getTestClass().getAnnotatedFieldValues(null, ExpectedExceptionClassRule.class, ExpectedException.class);
    Statement result = super.methodBlock(frameworkMethod);
    for (TestRule item : testRules) {
        result = item.apply(result, getDescription());
    }
    return result;
}}

然后我创建以下注释:
@Retention(RUNTIME)
@Target({ FIELD })
public @interface ExpectedExceptionClassRule {

}

最后,我能够和我的运行者一起运行测试:
@RunWith(SpringRunnerWithExpectedExceptionRule.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OverriddenProfilesTest {

    @ExpectedExceptionClassRule
    public static ExpectedException expectedException = ExpectedException.none();

    @BeforeClass
    public static void before() {
        expectedException.expectCause(runtimeExceptionMethod());
    }


    @Test
    public void testThatShouldThrowExceptionWhileSettingContext {
    }

    static Matcher<Throwable> runtimeExceptionMethod() {
        return new IsRuntimeException();
    }

    static class IsRuntimeException extends TypeSafeMatcher<Throwable> {
    //do stuff
    }

有关解决方案的更多信息,请访问 here .

关于spring-boot - SpringBootTest - 如何断言上下文加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429462/

相关文章:

java - Spring Boot 中多个项目中的错误处理程序

grails 2.4.4 集成测试不使用测试数据源

Javascript 监听器在转换为 slug 后将文本从第一个文本字段添加到第二个文本字段

java - 为什么 Spring ThreadPoolTask​​Executor 创建的线程比 CorePoolSize 和 MaxPoolSize 更多

java - Spring中的多种实现

automated-tests - 你使用 WaTiR 吗?

javascript - 使用 jquery 检查互联网是否处于事件状态

Android底部栏菜单onclick Action

java - Gradle 无法构建具有 Docker 依赖项的 JAR

spring - Spring 3.2 中发布的新 Spring MVC 测试框架是否测试 web.xml 配置?