I need my Spring Bootable WebApplication to restart in JUnit

Without going into a painful detail, I have a problem when I run Junit tests all at once. If I run their class after class, everything is fine! Otherwise, I am having problems because I cannot restart my WebApplication inbetween junit-test-class. This leads to the fact that I have Zookeeper server clients in my WebApplication application that freeze after shutting down and starting the Zookeeper server between classes. These Zookeeper server clients may take some time to re-synchronize with the server, resulting in unpredictable behavior ...

Is there a way to restart my SpringBootServletInitializer class by calling something in the @BeforeClass and @AfterClass methods of the 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);
    }
}
+4
source share
1 answer

You can use annotation @DirtiesContext.

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

+5
source

Source: https://habr.com/ru/post/1611805/


All Articles