Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need my Spring Boot WebApplication to restart in JUnit

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...

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

@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);
    }
}
like image 499
smuggledPancakes Avatar asked Oct 15 '15 14:10

smuggledPancakes


1 Answers

You could use the @DirtiesContext annotation.

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

like image 112
Damien O'Reilly Avatar answered Nov 14 '22 21:11

Damien O'Reilly