Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainerTest.testWithHostExposedPortAndExternalNetwork
Source:KafkaContainerTest.java
...100 testKafkaFunctionality(kafka.getBootstrapServers());101 }102 }103 @Test104 public void testWithHostExposedPortAndExternalNetwork() throws Exception {105 Testcontainers.exposeHostPorts(12345);106 try (KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE).withNetwork(Network.newNetwork())) {107 kafka.start();108 testKafkaFunctionality(kafka.getBootstrapServers());109 }110 }111 protected void testKafkaFunctionality(String bootstrapServers) throws Exception {112 testKafkaFunctionality(bootstrapServers, 1, 1);113 }114 protected void testKafkaFunctionality(String bootstrapServers, int partitions, int rf) throws Exception {115 try (116 AdminClient adminClient = AdminClient.create(ImmutableMap.of(117 AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers118 ));...
testWithHostExposedPortAndExternalNetwork
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.Network;3import org.testcontainers.utility.DockerImageName;4import java.util.HashMap;5import java.util.Map;6public class KafkaContainerTest {7 public void testWithHostExposedPortAndExternalNetwork() throws Exception {8 Map<String, String> env = new HashMap<>();
testWithHostExposedPortAndExternalNetwork
Using AI Code Generation
1public class TestcontainersTest {2 public void testWithHostExposedPortAndExternalNetwork() throws Exception {3 try (KafkaContainer kafka = new KafkaContainer()) {4 kafka.start();5 Map<String, String> env = new HashMap<>();6 env.put("KAFKA_BOOTSTRAP_SERVERS", kafka.getBootstrapServers());7 try (GenericContainer<?> consumer = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))8 .withEnv(env)9 .withNetwork(kafka.getNetwork())10 .withNetworkAliases("my-kafka")11 .withExposedPorts(9092)12 .withCommand("sh", "-c", "tail -f /dev/null")) {13 consumer.start();14 try (GenericContainer<?> producer = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))15 .withNetwork(kafka.getNetwork())16 .withCommand("kafka-console-producer",17 "--topic", "test-topic")) {18 producer.start();19 producer.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger("producer")));20 try (GenericContainer<?> consumer2 = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))21 .withNetwork(kafka.getNetwork())22 .withCommand("kafka-console-consumer",23 "--from-beginning")) {24 consumer2.start();25 consumer2.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger("consumer2")));26 producer.copyFileToContainer(MountableFile.forClasspathResource("test-message.txt"), "/test-message.txt");27 producer.execInContainer("bash", "-c", "cat /test-message.txt | kafka-console-producer --broker-list my-kafka:9092 --topic test-topic");28 Thread.sleep(1000);29 }30 }31 }32 }33 }34}35@RunWith(JUnitPlatform.class)36public class TestcontainersTest {
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!!