How to use KafkaContainer method of org.testcontainers.containers.KafkaContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainer.KafkaContainer

Source:KafkaTestContainer.java Github

copy

Full Screen

2import org.slf4j.Logger;3import org.slf4j.LoggerFactory;4import org.springframework.beans.factory.DisposableBean;5import org.springframework.beans.factory.InitializingBean;6import org.testcontainers.containers.KafkaContainer;7import org.testcontainers.containers.output.Slf4jLogConsumer;8import org.testcontainers.utility.DockerImageName;9public class KafkaTestContainer implements InitializingBean, DisposableBean {10 private KafkaContainer kafkaContainer;11 private static final Logger log = LoggerFactory.getLogger(KafkaTestContainer.class);12 @Override13 public void destroy() {14 if (null != kafkaContainer && kafkaContainer.isRunning()) {15 kafkaContainer.close();16 }17 }18 @Override19 public void afterPropertiesSet() {20 if (null == kafkaContainer) {21 kafkaContainer =22 new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.2.1"))23 .withLogConsumer(new Slf4jLogConsumer(log))24 .withReuse(true);25 }26 if (!kafkaContainer.isRunning()) {27 kafkaContainer.start();28 }29 }30 public KafkaContainer getKafkaContainer() {31 return kafkaContainer;32 }33}...

Full Screen

Full Screen

KafkaContainer

Using AI Code Generation

copy

Full Screen

1 public void testKafka() throws Exception {2 try (KafkaContainer kafka = new KafkaContainer()) {3 kafka.start();4 Properties properties = new Properties();5 properties.setProperty("bootstrap.servers", kafka.getBootstrapServers());6 properties.setProperty("key.serializer", StringSerializer.class.getName());7 properties.setProperty("value.serializer", StringSerializer.class.getName());8 properties.setProperty("key.deserializer", StringDeserializer.class.getName());9 properties.setProperty("value.deserializer", StringDeserializer.class.getName());10 properties.setProperty("auto.offset.reset", "earliest");11 try (KafkaProducer<String, String> producer = new KafkaProducer<>(properties);12 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties)) {13 String topic = "test-topic";14 consumer.subscribe(Collections.singleton(topic));15 producer.send(new ProducerRecord<>(topic, "test-key", "test-value"));16 ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(5));17 assertThat(records).isNotEmpty();18 assertThat(records.iterator().next().value()).isEqualTo("test-value");19 }20 }21 }22}23public class KafkaContainer extends GenericContainer<KafkaContainer> {24 private static final String KAFKA_IMAGE = "confluentinc/cp-kafka:5.1.1";25 private static final Integer KAFKA_PORT = 9092;26 private static final Integer ZOOKEEPER_PORT = 2181;27 private static final String STARTER_SCRIPT = "/testcontainers_start.sh";28 private static final String INTERNAL_HOST_NAME = "kafka";

Full Screen

Full Screen

KafkaContainer

Using AI Code Generation

copy

Full Screen

1KafkaContainer kafka = new KafkaContainer();2kafka.start();3ZookeeperContainer zookeeper = new ZookeeperContainer();4zookeeper.start();5KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper);6kafka.start();7KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper).withTopic("my_topic");8kafka.start();9KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper).withTopic("my_topic", 3);10kafka.start();11KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper).withTopic("my_topic", 3, (short) 1);12kafka.start();13KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper).withTopic("my_topic", 3, (short) 1, "min.insync.replicas=2");14kafka.start();15KafkaContainer kafka = new KafkaContainer().withEmbeddedZookeeper(zookeeper).withTopic("

Full Screen

Full Screen

KafkaContainer

Using AI Code Generation

copy

Full Screen

1 public void testKafkaContainer() throws ExecutionException, InterruptedException {2 try (KafkaContainer kafka = new KafkaContainer()) {3 kafka.start();4 Properties producerProps = new Properties();5 producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());6 producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);7 producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);8 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProps);9 Properties consumerProps = new Properties();10 consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());11 consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "consumer");12 consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");13 consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);14 consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);15 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps);16 consumer.subscribe(Collections.singletonList("topic"));17 ProducerRecord<String, String> record = new ProducerRecord<>("topic", "key", "value");18 producer.send(record).get();19 ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(5));20 assertThat(records.count()).isEqualTo(1);21 assertThat(records.iterator().next().value()).isEqualTo("value");22 }23 }24}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful