我需要我的 Spring Boot WebApplication 在 JUnit 中重新启动 [英] I need my Spring Boot WebApplication to restart in JUnit

查看:20
本文介绍了我需要我的 Spring Boot WebApplication 在 JUnit 中重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不深入细节的情况下,我在一次运行所有 Junit 测试时遇到了问题.如果我一个班地管理他们,一切都很棒!否则我会遇到麻烦,因为我无法在 junit-test-class 之间重新启动我的 WebApplication.这导致我在我的 WebApplication 中有 Zookeeper 服务器客户端,在我关闭和启动 Zookeeper 服务器后,这些客户端会在类之间挂起.这些 Zookeeper 服务器客户端可能需要一段时间才能与服务器重新同步,这会导致不可预测的行为......

Without going into excruciating detail, I am having an issue when I run my Junit tests all at once. If I run them class by class, everything is great! Otherwise I have trouble because I cannot restart my WebApplication inbetween junit-test-class. This causes me to have Zookeeper server clients in my WebApplication that hang around after I go through the shutdown and startup of the Zookeeper server in-between classes. Those Zookeeper server clients can take a while to resync with server and this causes unpredictable behavior...

有没有办法通过在 JUnit 测试的 @BeforeClass 和 @AfterClass 方法中调用一些东西来让我的 SpringBootServletInitializer 类重新启动?

Is there a way to have my SpringBootServletInitializer class restart by calling something in the @BeforeClass and @AfterClass methods of a JUnit test?

WebApplication.java

WebApplication.java

@ComponentScan
@EnableAutoConfiguration
@EnableWebMvc
@EnableHyperMediaSupport(...)
@PropertySources(...)
public class WebApplication extends SpringBootServletInitializer
{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
    {
        return builder.sources(WebApplication.class);
    }

    @Override
    protected WebApplicationContext run(SpringApplication application)
    {
        application.getSources().remove(ErrorPageFilter.class);
        return (WebApplicationContext) application.run();
    }

    public static void main(String[] args)
    {
        SpringApplication.run(WebApplication.class, args);
    }
}

推荐答案

您可以使用 @DirtiesContext 注释.

You could use the @DirtiesContext annotation.

这将提示 Spring Test runner 重新加载测试方法之间的上下文.

This will hint to the Spring Test runner to reload the context between test methods.

这篇关于我需要我的 Spring Boot WebApplication 在 JUnit 中重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆