0

I've noticed that test containers is restarting container. It happens when I am executing tests, between Test classes. First test class tests connects to container without issue, but then in next class port which is bind to host is incremented (cause new container is up), and my Spring integration test dosn't know that, which cause to rest test fails. How to persist container for all tests?

Please help

The container inside port (5432, as it is postgressql) is the same all the time.

masterdany88
  • 5,041
  • 11
  • 58
  • 132

1 Answers1

2

If you want to reuse containers between test classes you need to declare it as static field and initialize once, as example

@ContextConfiguration(initializers = BaseIntegrationTest.Initializer.class)
class BaseIntegrationTest {
  static KafkaContainer kafkaContainer;
  static {
    kafkaContainer = new KafkaContainer();
    kafkaContainer.start();
  }

  static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
      @Override
      public void initialize(ConfigurableApplicationContext applicationContext) {
        TestPropertyValues.of(
          "kafka.bootstrapServers=" + kafkaContainer.getBootstrapServers()
        ).applyTo(applicationContext);
      }
    }
}