All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.springboot.broker.kafka.KafkaTestContainerExtension.mustache Maven / Gradle / Ivy

There is a newer version: 1.23.0
Show newest version
package {{packageName}};

import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

public class KafkaTestContainerExtension implements BeforeAllCallback {

  private static final AtomicBoolean kafkaContainerStarted = new AtomicBoolean(false);
  private static KafkaContainer kafkaContainer;

  @Override
  public void beforeAll(final ExtensionContext context) {
    if (!kafkaContainerStarted.get()) {
      kafkaContainer = new KafkaContainer(DockerImageName.parse("{{kafkaDockerImage}}")).withNetwork(null);
      // Waiting this issue https://github.com/testcontainers/testcontainers-java/issues/9506 to remove this line
      kafkaContainer.withEnv("KAFKA_LISTENERS", "PLAINTEXT://:9092,BROKER://:9093,CONTROLLER://:9094");
      kafkaContainer.start();
      kafkaContainerStarted.set(true);
      System.setProperty("kafka.bootstrap.servers", kafkaContainer.getBootstrapServers());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy