Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainerTest.testWithHostExposedPort
Source:KafkaContainerTest.java
...43 testKafkaFunctionality(kafka.getBootstrapServers());44 }45 }46 @Test47 public void testWithHostExposedPort() throws Exception {48 Testcontainers.exposeHostPorts(12345);49 try (KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE)) {50 kafka.start();51 testKafkaFunctionality(kafka.getBootstrapServers());52 }53 }54 @Test55 public void testExternalZookeeperWithExternalNetwork() throws Exception {56 try (57 Network network = Network.newNetwork();58 // withExternalZookeeper {59 KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE)60 .withNetwork(network)61 .withExternalZookeeper("zookeeper:2181");...
testWithHostExposedPort
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import java.util.concurrent.TimeUnit;5import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;6public class KafkaContainerTest {7 public void testWithHostExposedPort() throws Exception {8 try (KafkaContainer kafka = new KafkaContainer()9 .withLogConsumer(new Slf4jLogConsumer(KafkaContainerTest.class.getSimpleName()))) {10 kafka.start();11 String host = kafka.getContainerIpAddress();12 int port = kafka.getMappedPort(9092);13 assertEquals("host is not localhost", "localhost", host);14 assertEquals("port is not 9092", 9092, port);15 }16 }17}18package org.testcontainers.containers;19import org.junit.Test;20import org.testcontainers.containers.output.Slf4jLogConsumer;21import java.util.concurrent.TimeUnit;22import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;23public class KafkaContainerTest {24 public void testWithHostExposedPort() throws Exception {25 try (KafkaContainer kafka = new KafkaContainer()26 .withLogConsumer(new Slf4jLogConsumer(KafkaContainerTest.class.getSimpleName()))) {27 kafka.start();28 String host = kafka.getContainerIpAddress();29 int port = kafka.getMappedPort(9092);30 assertEquals("host is not localhost", "localhost", host);31 assertEquals("port is not 9092", 9092, port);32 }33 }34}
testWithHostExposedPort
Using AI Code Generation
1 public void testWithHostExposedPort() throws Exception {2 try (KafkaContainer kafka = new KafkaContainer()) {3 kafka.start();4 String bootstrapServers = kafka.getBootstrapServers();5 assertThat(bootstrapServers, containsString("localhost"));6 }7 }8}9 public void testWithFixedExposedPort() throws Exception {10 try (KafkaContainer kafka = new KafkaContainer()11 .withExposedPorts(KAFKA_PORT)) {12 kafka.start();13 String bootstrapServers = kafka.getBootstrapServers();14 assertThat(bootstrapServers, containsString("localhost"));15 }16 }17}18 public void testWithCustomNetworkAliases() throws Exception {19 try (KafkaContainer kafka = new KafkaContainer()20 .withNetworkAliases("kafka")) {21 kafka.start();22 String bootstrapServers = kafka.getBootstrapServers();23 assertThat(bootstrapServers, containsString("kafka"));24 }25 }26}27 public void testWithCustomConfiguration() throws Exception {28 try (KafkaContainer kafka = new KafkaContainer()29 .withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1")30 .withEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1")31 .withEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1")) {32 kafka.start();33 String bootstrapServers = kafka.getBootstrapServers();34 Properties config = new Properties();35 config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);36 config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);37 config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);38 config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);39 config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);40 config.put(ConsumerConfig.GROUP_ID_CONFIG, "testWithCustomConfiguration");
testWithHostExposedPort
Using AI Code Generation
1import java.util.Properties;2import org.apache.kafka.clients.consumer.KafkaConsumer;3import org.apache.kafka.clients.producer.KafkaProducer;4import org.apache.kafka.clients.producer.ProducerRecord;5import org.apache.kafka.common.serialization.StringDeserializer;6import org.apache.kafka.common.serialization.StringSerializer;7import org.junit.Test;8import org.testcontainers.containers.KafkaContainer;9import org.testcontainers.containers.Network;10public class KafkaContainerTest {11 public void testWithHostExposedPort() throws Exception {12 Network network = Network.newNetwork();13 KafkaContainer kafka = new KafkaContainer("5.1.1")14 .withNetwork(network)15 .withNetworkAliases("kafka");16 kafka.start();17 Properties producerProps = new Properties();18 producerProps.setProperty("bootstrap.servers", kafka.getBootstrapServers());19 producerProps.setProperty("key.serializer", StringSerializer.class.getName());20 producerProps.setProperty("value.serializer", StringSerializer.class.getName());21 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProps);22 producer.send(new ProducerRecord<>("topic1", "key1", "value1"));23 producer.close();24 Properties consumerProps = new Properties();25 consumerProps.setProperty("bootstrap.servers", kafka.getBootstrapServers());26 consumerProps.setProperty("key.deserializer", StringDeserializer.class.getName());27 consumerProps.setProperty("value.deserializer", StringDeserializer.class.getName());28 consumerProps.setProperty("group.id", "test");29 consumerProps.setProperty("auto.offset.reset", "earliest");30 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps);31 consumer.subscribe(java.util.Collections.singletonList("topic1"));32 consumer.poll(1000);33 consumer.close();34 }35}36 at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)37 at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:312)38 at org.testcontainers.containers.KafkaContainer.start(KafkaContainer.java:51)39 at org.testcontainers.containers.KafkaContainerTest.testWithHostExposedPort(KafkaContainerTest.java:25)
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!!