Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainer.withEmbeddedZookeeper
Source:ContainerLocalKafkaService.java
...30 public ContainerLocalKafkaService(KafkaContainer kafka) {31 this.kafka = kafka;32 }33 protected KafkaContainer initContainer() {34 return new KafkaContainer().withEmbeddedZookeeper();35 }36 public String getBootstrapServers() {37 return kafka.getBootstrapServers();38 }39 @Override40 public void registerProperties() {41 System.setProperty(KafkaProperties.KAFKA_BOOTSTRAP_SERVERS, getBootstrapServers());42 }43 @Override44 public void initialize() {45 kafka.start();46 registerProperties();47 LOG.info("Kafka bootstrap server running at address {}", kafka.getBootstrapServers());48 }49 @Override50 public void shutdown() {51 kafka.stop();52 }53 @Override54 public KafkaContainer getContainer() {55 return kafka;56 }57 public static ContainerLocalKafkaService kafka2Container() {58 return new ContainerLocalKafkaService();59 }60 public static ContainerLocalKafkaService kafka3Container() {61 KafkaContainer container = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.0"));62 container = container.withEmbeddedZookeeper();63 return new ContainerLocalKafkaService(container);64 }65}...
Source:KafkaTestContainerSetup.java
...16 public static void initTestContainers(ConfigurableEnvironment configEnv) {17 if(kafkaContainer.isRunning()){18 return;19 }20 kafkaContainer.withEmbeddedZookeeper();21 KAFKA_LOGGER.info("Stating kafka test container");22 kafkaContainer.start();23 kafkaContainer.followOutput(new Slf4jLogConsumer(KAFKA_LOGGER));24 }25 public static String getBootstrapServers() {26 return kafkaContainer.getBootstrapServers();27 }28}...
Source:KafkaContainerExtension.java
...12 public void beforeAll(ExtensionContext extensionContext) {13 KAFKA = new org.testcontainers.containers.KafkaContainer(DockerImageName14 .parse(CONFLUENT_IMAGE_NAME)15 .withTag(CONFLUENT_PLATFORM_VERSION))16 .withEmbeddedZookeeper();17 KAFKA.start();18 }19 @Override20 public void afterAll(ExtensionContext extensionContext) throws Exception {21 KAFKA.stop();22 }23}
withEmbeddedZookeeper
Using AI Code Generation
1import java.util.Properties;2import org.apache.kafka.clients.consumer.ConsumerConfig;3import org.apache.kafka.clients.consumer.KafkaConsumer;4import org.apache.kafka.clients.producer.KafkaProducer;5import org.apache.kafka.clients.producer.ProducerConfig;6import org.apache.kafka.common.serialization.StringDeserializer;7import org.apache.kafka.common.serialization.StringSerializer;8import org.testcontainers.containers.KafkaContainer;9import org.testcontainers.containers.Network;10import org.testcontainers.containers.output.Slf4jLogConsumer;11import org.testcontainers.utility.DockerImageName;12public class Main {13 public static void main(String[] args) {14 try (KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))15 .withEmbeddedZookeeper()) {16 kafkaContainer.start();17 System.out.println("Kafka started");18 Network network = kafkaContainer.getNetwork();19 String bootstrapServers = network.aliasFor(kafkaContainer.getNetworkAliases().get(0)) + ":9092";20 System.out.println("Bootstrap servers: " + bootstrapServers);21 Properties producerProps = new Properties();22 producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);23 producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);24 producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);25 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProps);26 Properties consumerProps = new Properties();27 consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);28 consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test");29 consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);30 consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);31 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps);32 consumer.subscribe(List.of("test"));33 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LoggerFactory.getLogger("testcontainers"));34 kafkaContainer.followOutput(logConsumer);35 producer.send(new ProducerRecord<>("test", "test", "test"));36 ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(5));37 for (ConsumerRecord<String, String> record : records) {38 System.out.println("Received: " + record.value());39 }40 }41 }42}
withEmbeddedZookeeper
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.Network;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.utility.DockerImageName;5import java.time.Duration;6import java.util.concurrent.TimeUnit;7public class KafkaTestContainer {8 public static void main(String[] args) throws Exception {9 Network network = Network.newNetwork();10 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(System.out);11 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.2"))12 .withEmbeddedZookeeper()13 .withNetwork(network)14 .withNetworkAliases("kafka")15 .withStartupTimeout(Duration.ofMinutes(2))16 .withLogConsumer(logConsumer);17 kafka.start();18 kafka.execInContainer("kafka-topics", "--create", "--topic", "test", "--partitions", "1", "--replication-factor", "1", "--if-not-exists", "--zookeeper", "localhost:2181");19 kafka.execInContainer("kafka-topics", "--describe", "--topic", "test", "--zookeeper", "localhost:2181");20 kafka.execInContainer("kafka-console-producer", "--topic", "test", "--broker-list", "localhost:9092");21 kafka.execInContainer("kafka-console-consumer", "--topic", "test", "--from-beginning", "--bootstrap-server", "localhost:9092");22 TimeUnit.MINUTES.sleep(10);23 kafka.stop();24 }25}26import org.testcontainers.containers.KafkaContainer;27import org.testcontainers.containers.Network;28import org.testcontainers.containers.output.Slf4jLogConsumer;29import org.testcontainers.utility.DockerImageName;30import java.time.Duration;31import java.util.concurrent.TimeUnit;32public class KafkaTestContainer {33 public static void main(String[] args) throws Exception {34 Network network = Network.newNetwork();35 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(System.out);36 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.2"))37 .withNetwork(network)38 .withNetworkAliases("kafka")
withEmbeddedZookeeper
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.utility.DockerImageName;3public class KafkaContainerTest {4 public static void main(String[] args) {5 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));6 kafka.withEmbeddedZookeeper();7 kafka.start();8 System.out.println("Kafka started");9 System.out.println("Kafka bootstrap server: " + kafka.getBootstrapServers());10 kafka.stop();11 }12}
withEmbeddedZookeeper
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.utility.DockerImageName;3public class KafkaContainerTest {4 public static void main(String[] args) {5 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.0.1"));6 kafkaContainer.withEmbeddedZookeeper();7 kafkaContainer.start();8 System.out.println(kafkaContainer.getBootstrapServers());9 }10}11import org.testcontainers.containers.KafkaContainer;12import org.testcontainers.utility.DockerImageName;13public class KafkaContainerTest {14 public static void main(String[] args) {15 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.0.1"));16 kafkaContainer.withEmbeddedZookeeper();17 kafkaContainer.start();18 System.out.println(kafkaContainer.getBootstrapServers());19 kafkaContainer.stop();20 }21}22import org.testcontainers.containers.KafkaContainer;23import org.testcontainers.utility.DockerImageName;24public class KafkaContainerTest {25 public static void main(String[] args) {26 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.0.1"));27 kafkaContainer.withEmbeddedZookeeper();28 kafkaContainer.start();29 System.out.println(kafkaContainer.getBootstrapServers());30 kafkaContainer.stop();31 kafkaContainer.start();32 System.out.println(kafkaContainer.getBootstrapServers());33 }34}35import org.testcontainers.containers.KafkaContainer;36import org.testcontainers.utility.DockerImageName;37public class KafkaContainerTest {38 public static void main(String[] args) {39 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.0.1"));40 kafkaContainer.withEmbeddedZookeeper();41 kafkaContainer.start();42 System.out.println(kafkaContainer.getBootstrapServers());43 kafkaContainer.stop();44 kafkaContainer.start();45 System.out.println(kafkaContainer.getBootstrapServers());
withEmbeddedZookeeper
Using AI Code Generation
1import org.junit.Test;2import org.testcontainers.containers.KafkaContainer;3public class TestKafkaContainer {4 public void testKafkaContainer() throws Exception {5 try (KafkaContainer kafka = new KafkaContainer()) {6 kafka.start();7 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());8 }9 }10}11import org.junit.Test;12import org.testcontainers.containers.KafkaContainer;13public class TestKafkaContainer {14 public void testKafkaContainer() throws Exception {15 try (KafkaContainer kafka = new KafkaContainer()16 .withEmbeddedZookeeper()) {17 kafka.start();18 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());19 }20 }21}22import org.junit.Test;23import org.testcontainers.containers.KafkaContainer;24public class TestKafkaContainer {25 public void testKafkaContainer() throws Exception {26 try (KafkaContainer kafka = new KafkaContainer()27 .withEmbeddedZookeeper()28 .withNetwork(null)) {29 kafka.start();30 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());31 }32 }33}34import org.junit.Test;35import org.testcontainers.containers.KafkaContainer;36public class TestKafkaContainer {37 public void testKafkaContainer() throws Exception {38 try (KafkaContainer kafka = new KafkaContainer()39 .withEmbeddedZookeeper()40 .withNetwork(null)41 .withNetworkAliases("kafka")) {42 kafka.start();43 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());44 }45 }46}47import org.junit.Test;48import org.testcontainers.containers.KafkaContainer;49public class TestKafkaContainer {50 public void testKafkaContainer() throws Exception {51 try (KafkaContainer kafka
withEmbeddedZookeeper
Using AI Code Generation
1package org.testcontainers.containers;2import java.util.Collections;3import org.junit.Test;4import org.testcontainers.containers.KafkaContainer;5import org.testcontainers.utility.DockerImageName;6public class KafkaContainerTest {7 public void testKafka() {8 try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))9 .withEmbeddedZookeeper()) {10 kafka.start();11 System.out.println(kafka.getBootstrapServers());12 }13 }14}15package org.testcontainers.containers;16import java.util.Collections;17import org.junit.Test;18import org.testcontainers.containers.KafkaContainer;19import org.testcontainers.utility.DockerImageName;20public class KafkaContainerTest {21 public void testKafka() {22 try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))23 .withEmbeddedZookeeper()) {24 kafka.start();25 System.out.println(kafka.getBootstrapServers());26 }27 }28}29package org.testcontainers.containers;30import java.util.Collections;31import org.junit.Test;32import org.testcontainers.containers.KafkaContainer;33import org.testcontainers.utility.DockerImageName;34public class KafkaContainerTest {35 public void testKafka() {36 try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))37 .withEmbeddedZookeeper()) {38 kafka.start();39 System.out.println(kafka.getBootstrapServers());40 }41 }42}43package org.testcontainers.containers;44import java.util.Collections;45import org.junit.Test;46import org.testcontainers.containers.KafkaContainer;47import org.testcontainers.utility.DockerImageName;48public class KafkaContainerTest {49 public void testKafka() {50 try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluent
withEmbeddedZookeeper
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.Network;3import org.testcontainers.utility.DockerImageName;4public class KafkaTestContainer {5 public static void main(String[] args) {6 try (KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.0"))7 .withEmbeddedZookeeper()) {8 kafka.start();9 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());10 }11 }12}13import org.testcontainers.containers.KafkaContainer;14import org.testcontainers.containers.Network;15import org.testcontainers.containers.ZookeeperContainer;16import org.testcontainers.utility.DockerImageName;17public class KafkaTestContainer {18 public static void main(String[] args) {19 Network network = Network.newNetwork();20 try (ZookeeperContainer zookeeper = new ZookeeperContainer(DockerImageName.parse("confluentinc/cp-zookeeper:5.5.0"))21 .withNetwork(network);22 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.0"))23 .withNetwork(network)) {24 zookeeper.start();25 kafka.start();26 System.out.println("Kafka bootstrap servers: " + kafka.getBootstrapServers());27 }28 }29}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!