From c6af09fcc02e68f8513e0111416d93153b199235 Mon Sep 17 00:00:00 2001 From: Thiago Date: Wed, 5 Nov 2025 23:21:07 +0100 Subject: [PATCH 1/4] BAEL-9420: Add Testcontainer Podman Test --- pom.xml | 2 +- testing-modules/pom.xml | 1 + .../test-containers-podman/pom.xml | 129 ++++++++++++++++++ .../KafkaIntegrationTest.java | 58 ++++++++ .../MySQLIntegrationTest.java | 33 +++++ .../RedisIntegrationTest.java | 29 ++++ 6 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 testing-modules/test-containers-podman/pom.xml create mode 100644 testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java create mode 100644 testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java create mode 100644 testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java diff --git a/pom.xml b/pom.xml index 40e821558ef1..bd021b14e417 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,6 @@ parent-modules 1.0.0-SNAPSHOT parent-modules - pom @@ -873,6 +872,7 @@ xml xstream libraries-data-io-2 + testing-modules/test-containers-podman diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 61e043f7e4f2..e8951a0100e6 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -61,6 +61,7 @@ mockito-3 testing-techniques gatling-java + test-containers-podman diff --git a/testing-modules/test-containers-podman/pom.xml b/testing-modules/test-containers-podman/pom.xml new file mode 100644 index 000000000000..74985b2542a4 --- /dev/null +++ b/testing-modules/test-containers-podman/pom.xml @@ -0,0 +1,129 @@ + + + 4.0.0 + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + test-containers-podman + 1.0-SNAPSHOT + test-containers-podman + Test Containers Using Podman + jar + + + 2.0.1 + 2.2.4 + + + + + org.testcontainers + testcontainers + ${testcontainers.version} + test + + + org.testcontainers + junit-jupiter + 1.21.3 + test + + + org.testcontainers + testcontainers-mysql + ${testcontainers.version} + test + + + org.testcontainers + kafka + 1.21.3 + test + + + com.redis + testcontainers-redis + ${redis.testcontainers.version} + test + + + + redis.clients + jedis + 5.1.0 + test + + + + org.junit.jupiter + junit-jupiter-engine + 5.10.2 + test + + + + com.mysql + mysql-connector-j + 9.5.0 + test + + + + org.apache.kafka + kafka-clients + 3.6.1 + test + + + + + + + + src/test/resources + true + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + unix://${env.XDG_RUNTIME_DIR}/podman/podman.sock + true + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + unix://${env.XDG_RUNTIME_DIR}/podman/podman.sock + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + + diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java new file mode 100644 index 000000000000..186eab23701d --- /dev/null +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java @@ -0,0 +1,58 @@ +package com.baeldung.testcontainers.podman; + +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.serialization.StringDeserializer; +import org.apache.kafka.common.serialization.StringSerializer; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.utility.DockerImageName; + +import java.time.Duration; +import java.util.Collections; +import java.util.Properties; + +public class KafkaIntegrationTest { + +@Test +void whenProducingMessage_thenConsumerReceivesIt() { + DockerImageName image = DockerImageName.parse("confluentinc/cp-kafka:7.6.1"); + try (KafkaContainer kafka = new KafkaContainer(image)) { + kafka.start(); + + String bootstrap = kafka.getBootstrapServers(); + String topic = "hello"; + + Properties prodProps = new Properties(); + prodProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); + prodProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + prodProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + + try (KafkaProducer producer = new KafkaProducer<>(prodProps)) { + producer.send(new ProducerRecord<>(topic, "key", "hello")).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + Properties consProps = new Properties(); + consProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); + consProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); + consProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); + consProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + consProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + + try (KafkaConsumer consumer = new KafkaConsumer<>(consProps)) { + consumer.subscribe(Collections.singletonList(topic)); + ConsumerRecords records = consumer.poll(Duration.ofSeconds(10)); + ConsumerRecord first = records.iterator().next(); + Assertions.assertEquals("hello", first.value()); + } + } +} +} diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java new file mode 100644 index 000000000000..6b9e9a02c457 --- /dev/null +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.testcontainers.podman; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.testcontainers.mysql.MySQLContainer; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +class MySQLIntegrationTest { + +@Test +void whenQueryingDatabase_thenReturnsOne() throws Exception { + + try (MySQLContainer mysql = new MySQLContainer("mysql:8.4")) { + mysql.start(); + + try (Connection conn = DriverManager.getConnection( + mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword()); + Statement st = conn.createStatement()) { + + st.execute("CREATE TABLE t(id INT PRIMARY KEY)"); + st.execute("INSERT INTO t VALUES (1)"); + ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM t"); + rs.next(); + Assertions.assertEquals(1, rs.getInt(1)); + } + } +} + +} diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java new file mode 100644 index 000000000000..9ebc08bd9a1a --- /dev/null +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java @@ -0,0 +1,29 @@ +package com.baeldung.testcontainers.podman; + + +import com.redis.testcontainers.RedisContainer; +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import redis.clients.jedis.Jedis; + +class RedisIntegrationTest { + +@Test +void whenSettingValue_thenCanGetItBack() { + try (RedisContainer redis = new RedisContainer("redis:7-alpine").withExposedPorts(6379)) { + redis.start(); + + String host = redis.getHost(); + int port = redis.getFirstMappedPort(); + + try (Jedis jedis = new Jedis(host, port)) { + jedis.set("greeting", "hello"); + String value = jedis.get("greeting"); + Assertions.assertEquals("hello", value); + } + } +} + + +} From e91ed0285af218fe24f8e7b3a072dc6246b92c15 Mon Sep 17 00:00:00 2001 From: Thiago Date: Fri, 7 Nov 2025 16:39:17 +0100 Subject: [PATCH 2/4] Rename test classes from *IntegrationTest to *LiveTest - Rename KafkaIntegrationTest to KafkaLiveTest - Rename MySQLIntegrationTest to MysqlLiveTest - Rename RedisIntegrationTest to RedisLiveTest - Update class names to match new file names - Remove unused import in RedisLiveTest --- .../{KafkaIntegrationTest.java => KafkaLiveTest.java} | 2 +- .../{MySQLIntegrationTest.java => MysqlLiveTest.java} | 2 +- .../{RedisIntegrationTest.java => RedisLiveTest.java} | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) rename testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/{KafkaIntegrationTest.java => KafkaLiveTest.java} (98%) rename testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/{MySQLIntegrationTest.java => MysqlLiveTest.java} (96%) rename testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/{RedisIntegrationTest.java => RedisLiveTest.java} (92%) diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java similarity index 98% rename from testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java rename to testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java index 186eab23701d..bdc7ce2e88cd 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaIntegrationTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java @@ -18,7 +18,7 @@ import java.util.Collections; import java.util.Properties; -public class KafkaIntegrationTest { +public class KafkaLiveTest { @Test void whenProducingMessage_thenConsumerReceivesIt() { diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java similarity index 96% rename from testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java rename to testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java index 6b9e9a02c457..23b92e2a988f 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MySQLIntegrationTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java @@ -9,7 +9,7 @@ import java.sql.ResultSet; import java.sql.Statement; -class MySQLIntegrationTest { +class MysqlLiveTest { @Test void whenQueryingDatabase_thenReturnsOne() throws Exception { diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java similarity index 92% rename from testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java rename to testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java index 9ebc08bd9a1a..b926e1e6fba7 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisIntegrationTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java @@ -2,12 +2,11 @@ import com.redis.testcontainers.RedisContainer; -import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import redis.clients.jedis.Jedis; -class RedisIntegrationTest { +class RedisLiveTest { @Test void whenSettingValue_thenCanGetItBack() { From 34733e6d3b25c9e604eece2b511036b0767ed769 Mon Sep 17 00:00:00 2001 From: Thiago Date: Sat, 8 Nov 2025 22:38:21 +0100 Subject: [PATCH 3/4] fix --- .../KafkaLiveTest.java | 66 +++++++++---------- .../MysqlLiveTest.java | 33 +++++----- .../RedisLiveTest.java | 28 ++++---- 3 files changed, 62 insertions(+), 65 deletions(-) diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java index bdc7ce2e88cd..d6a3e3cab0b8 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/KafkaLiveTest.java @@ -20,39 +20,39 @@ public class KafkaLiveTest { -@Test -void whenProducingMessage_thenConsumerReceivesIt() { - DockerImageName image = DockerImageName.parse("confluentinc/cp-kafka:7.6.1"); - try (KafkaContainer kafka = new KafkaContainer(image)) { - kafka.start(); - - String bootstrap = kafka.getBootstrapServers(); - String topic = "hello"; - - Properties prodProps = new Properties(); - prodProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); - prodProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); - prodProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); - - try (KafkaProducer producer = new KafkaProducer<>(prodProps)) { - producer.send(new ProducerRecord<>(topic, "key", "hello")).get(); - } catch (Exception e) { - throw new RuntimeException(e); - } - - Properties consProps = new Properties(); - consProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); - consProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); - consProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); - consProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); - consProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); - - try (KafkaConsumer consumer = new KafkaConsumer<>(consProps)) { - consumer.subscribe(Collections.singletonList(topic)); - ConsumerRecords records = consumer.poll(Duration.ofSeconds(10)); - ConsumerRecord first = records.iterator().next(); - Assertions.assertEquals("hello", first.value()); + @Test + void whenProducingMessage_thenConsumerReceivesIt() { + DockerImageName image = DockerImageName.parse("confluentinc/cp-kafka:7.6.1"); + try (KafkaContainer kafka = new KafkaContainer(image)) { + kafka.start(); + + String bootstrap = kafka.getBootstrapServers(); + String topic = "hello"; + + Properties prodProps = new Properties(); + prodProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); + prodProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + prodProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + + try (KafkaProducer producer = new KafkaProducer<>(prodProps)) { + producer.send(new ProducerRecord<>(topic, "key", "hello")).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + Properties consProps = new Properties(); + consProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap); + consProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); + consProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); + consProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + consProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); + + try (KafkaConsumer consumer = new KafkaConsumer<>(consProps)) { + consumer.subscribe(Collections.singletonList(topic)); + ConsumerRecords records = consumer.poll(Duration.ofSeconds(10)); + ConsumerRecord first = records.iterator().next(); + Assertions.assertEquals("hello", first.value()); + } } } } -} diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java index 23b92e2a988f..e6df37781ba1 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/MysqlLiveTest.java @@ -11,23 +11,22 @@ class MysqlLiveTest { -@Test -void whenQueryingDatabase_thenReturnsOne() throws Exception { - - try (MySQLContainer mysql = new MySQLContainer("mysql:8.4")) { - mysql.start(); - - try (Connection conn = DriverManager.getConnection( - mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword()); - Statement st = conn.createStatement()) { - - st.execute("CREATE TABLE t(id INT PRIMARY KEY)"); - st.execute("INSERT INTO t VALUES (1)"); - ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM t"); - rs.next(); - Assertions.assertEquals(1, rs.getInt(1)); + @Test + void whenQueryingDatabase_thenReturnsOne() throws Exception { + + try (MySQLContainer mysql = new MySQLContainer("mysql:8.4")) { + mysql.start(); + + try (Connection conn = DriverManager.getConnection( + mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword()); + Statement st = conn.createStatement()) { + + st.execute("CREATE TABLE t(id INT PRIMARY KEY)"); + st.execute("INSERT INTO t VALUES (1)"); + ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM t"); + rs.next(); + Assertions.assertEquals(1, rs.getInt(1)); + } } } } - -} diff --git a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java index b926e1e6fba7..02f9075279b3 100644 --- a/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java +++ b/testing-modules/test-containers-podman/src/test/java/com.baeldung.testcontainers.podman/RedisLiveTest.java @@ -8,21 +8,19 @@ class RedisLiveTest { -@Test -void whenSettingValue_thenCanGetItBack() { - try (RedisContainer redis = new RedisContainer("redis:7-alpine").withExposedPorts(6379)) { - redis.start(); - - String host = redis.getHost(); - int port = redis.getFirstMappedPort(); - - try (Jedis jedis = new Jedis(host, port)) { - jedis.set("greeting", "hello"); - String value = jedis.get("greeting"); - Assertions.assertEquals("hello", value); + @Test + void whenSettingValue_thenCanGetItBack() { + try (RedisContainer redis = new RedisContainer("redis:7-alpine").withExposedPorts(6379)) { + redis.start(); + + String host = redis.getHost(); + int port = redis.getFirstMappedPort(); + + try (Jedis jedis = new Jedis(host, port)) { + jedis.set("greeting", "hello"); + String value = jedis.get("greeting"); + Assertions.assertEquals("hello", value); + } } } } - - -} From f1fff5627f6227a691c8a76a3bd528c0566849b4 Mon Sep 17 00:00:00 2001 From: Thiago Date: Tue, 11 Nov 2025 21:04:42 +0100 Subject: [PATCH 4/4] Fix duplicate module declaration in reactor Remove test-containers-podman from root pom.xml as it's already declared in testing-modules/pom.xml. This fixes the Maven reactor duplication error. --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 444f0441d749..89c030428d20 100644 --- a/pom.xml +++ b/pom.xml @@ -840,7 +840,6 @@ web-modules webrtc xml-modules - testing-modules/test-containers-podman