Best Testcontainers-java code snippet using com.example.kafkacluster.KafkaContainerClusterTest.testKafkaFunctionality
Source:KafkaContainerClusterTest.java
...29 ) {30 cluster.start();31 String bootstrapServers = cluster.getBootstrapServers();32 assertThat(cluster.getBrokers()).hasSize(3);33 testKafkaFunctionality(bootstrapServers, 3, 2);34 }35 }36 protected void testKafkaFunctionality(String bootstrapServers, int partitions, int rf) throws Exception {37 try (38 AdminClient adminClient = AdminClient.create(ImmutableMap.of(39 AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers40 ));41 KafkaProducer<String, String> producer = new KafkaProducer<>(42 ImmutableMap.of(43 ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,44 ProducerConfig.CLIENT_ID_CONFIG, UUID.randomUUID().toString()45 ),46 new StringSerializer(),47 new StringSerializer()48 );49 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(50 ImmutableMap.of(...
testKafkaFunctionality
Using AI Code Generation
1 public void testKafkaCluster() throws Exception {2 testKafkaFunctionality();3 }4}5package com.example.kafkacluster;6import org.apache.kafka.clients.consumer.Consumer;7import org.apache.kafka.clients.consumer.ConsumerConfig;8import org.apache.kafka.clients.consumer.ConsumerRecords;9import org.apache.kafka.clients.consumer.KafkaConsumer;10import org.apache.kafka.clients.producer.KafkaProducer;11import org.apache.kafka.clients.producer.Producer;12import org.apache.kafka.clients.producer.ProducerConfig;13import org.apache.kafka.clients.producer.ProducerRecord;14import org.apache.kafka.common.serialization.StringDeserializer;15import org.apache.kafka.common.serialization.StringSerializer;16import org.junit.Test;17import org.slf4j.Logger;18import org.slf4j.LoggerFactory;19import org.testcontainers.containers.KafkaContainer;20import org.testcontainers.containers.Network;21import org.testcontainers.containers.output.Slf4jLogConsumer;22import org.testcontainers.utility.DockerImageName;23import java.time.Duration;24import java.util.Collections;25import java.util.Properties;26import java.util.concurrent.ExecutionException;27public class KafkaContainerClusterTest {28 private static final Logger logger = LoggerFactory.getLogger(KafkaContainerClusterTest.class);29 public void testKafkaFunctionality() throws ExecutionException, InterruptedException {30 Network network = Network.newNetwork();31 KafkaContainer zookeeperContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-zookeeper:5.5.1"));32 zookeeperContainer.withNetwork(network);33 zookeeperContainer.withNetworkAliases("zookeeper");34 zookeeperContainer.start();35 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));36 kafkaContainer.withNetwork(network);37 kafkaContainer.withNetworkAliases("kafka");38 kafkaContainer.start();39 Properties producerProperties = new Properties();40 producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());41 producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());42 producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
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!!