javaspringpostconstructpredestroy

Spring issue with @PostConstruct and @PreDestroy


I have a Spring application that I am trying to test with EmbededRedis. So I created a component like below to Initialize and kill redis after test.

@Component
public class EmbededRedis {

  @Value("${spring.redis.port}")
  private int redisPort;

  private RedisServer redisServer;

  @PostConstruct
  public void startRedis() throws IOException {
    redisServer = new RedisServer(redisPort);
    redisServer.start();
  }

  @PreDestroy
  public void stopRedis() {
    redisServer.stop();
  }
}

But now I am facing a weird issue. Because spring caches the context, PreDestroy doesnt get called everytime after my test is executed, but for some reason, @PostConstruct gets called, and EmbededRedis tries to start the running redis server again and again, which is creatimg issues in the execution.

Is there a way to handle this situation by any mean?

Update This is how I am primarily defining my tests.

@SpringBootTest(classes = {SpringApplication.class})
@ActiveProfiles("test")
public class RedisApplicationTest {

Solution

  • Ditch the class and write an @Configuration class which exposed RedisServer as a bean.

    @Configuration
    public void EmbeddedRedisConfiguration {
    
       @Bean(initMethod="start", destroyMethod="stop")
       public RedisServer embeddedRedisServer(@Value("${spring.redis.port}") int port) {
          return new RedisServer(port);
       }
    }