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

查看:200
本文介绍了我需要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);
    }
}

推荐答案

您可以使用

You could use the @DirtiesContext annotation.

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

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

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

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