Skip to content

Commit 4f63806

Browse files
authored
BAEL-9420: Add Testcontainer Podman Test (#18914)
* BAEL-9420: Add Testcontainer Podman Test * 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 * fix * 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.
1 parent 72a42ee commit 4f63806

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

testing-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<module>mockito-3</module>
7777
<module>mockito-4</module>
7878
<module>gatling-java</module>
79+
<module>test-containers-podman</module>
7980
</modules>
8081

8182
</project>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>testing-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>test-containers-podman</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
<name>test-containers-podman</name>
16+
<description>Test Containers Using Podman</description>
17+
<packaging>jar</packaging>
18+
19+
<properties>
20+
<testcontainers.version>2.0.1</testcontainers.version>
21+
<redis.testcontainers.version>2.2.4</redis.testcontainers.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.testcontainers</groupId>
27+
<artifactId>testcontainers</artifactId>
28+
<version>${testcontainers.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.testcontainers</groupId>
33+
<artifactId>junit-jupiter</artifactId>
34+
<version>1.21.3</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.testcontainers</groupId>
39+
<artifactId>testcontainers-mysql</artifactId>
40+
<version>${testcontainers.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.testcontainers</groupId>
45+
<artifactId>kafka</artifactId>
46+
<version>1.21.3</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.redis</groupId>
51+
<artifactId>testcontainers-redis</artifactId>
52+
<version>${redis.testcontainers.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>redis.clients</groupId>
58+
<artifactId>jedis</artifactId>
59+
<version>5.1.0</version>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.junit.jupiter</groupId>
65+
<artifactId>junit-jupiter-engine</artifactId>
66+
<version>5.10.2</version>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>com.mysql</groupId>
72+
<artifactId>mysql-connector-j</artifactId>
73+
<version>9.5.0</version>
74+
<scope>test</scope>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>org.apache.kafka</groupId>
79+
<artifactId>kafka-clients</artifactId>
80+
<version>3.6.1</version>
81+
<scope>test</scope>
82+
</dependency>
83+
84+
</dependencies>
85+
86+
<build>
87+
<resources>
88+
<resource>
89+
<directory>src/test/resources</directory>
90+
<filtering>true</filtering>
91+
</resource>
92+
</resources>
93+
94+
<plugins>
95+
<!-- Unit tests -->
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-surefire-plugin</artifactId>
99+
<configuration>
100+
<environmentVariables>
101+
<!-- Podman socket in rootless mode -->
102+
<DOCKER_HOST>unix://${env.XDG_RUNTIME_DIR}/podman/podman.sock</DOCKER_HOST>
103+
<TESTCONTAINERS_RYUK_DISABLED>true</TESTCONTAINERS_RYUK_DISABLED>
104+
</environmentVariables>
105+
</configuration>
106+
</plugin>
107+
108+
<!-- Integration tests (if you use Failsafe) -->
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-failsafe-plugin</artifactId>
112+
<configuration>
113+
<environmentVariables>
114+
<DOCKER_HOST>unix://${env.XDG_RUNTIME_DIR}/podman/podman.sock</DOCKER_HOST>
115+
<TESTCONTAINERS_RYUK_DISABLED>true</TESTCONTAINERS_RYUK_DISABLED>
116+
</environmentVariables>
117+
</configuration>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.apache.maven.plugins</groupId>
121+
<artifactId>maven-compiler-plugin</artifactId>
122+
<configuration>
123+
<source>9</source>
124+
<target>9</target>
125+
</configuration>
126+
</plugin>
127+
</plugins>
128+
</build>
129+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.testcontainers.podman;
2+
3+
import org.apache.kafka.clients.consumer.ConsumerConfig;
4+
import org.apache.kafka.clients.consumer.ConsumerRecord;
5+
import org.apache.kafka.clients.consumer.ConsumerRecords;
6+
import org.apache.kafka.clients.consumer.KafkaConsumer;
7+
import org.apache.kafka.clients.producer.KafkaProducer;
8+
import org.apache.kafka.clients.producer.ProducerConfig;
9+
import org.apache.kafka.clients.producer.ProducerRecord;
10+
import org.apache.kafka.common.serialization.StringDeserializer;
11+
import org.apache.kafka.common.serialization.StringSerializer;
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
import org.testcontainers.containers.KafkaContainer;
15+
import org.testcontainers.utility.DockerImageName;
16+
17+
import java.time.Duration;
18+
import java.util.Collections;
19+
import java.util.Properties;
20+
21+
public class KafkaLiveTest {
22+
23+
@Test
24+
void whenProducingMessage_thenConsumerReceivesIt() {
25+
DockerImageName image = DockerImageName.parse("confluentinc/cp-kafka:7.6.1");
26+
try (KafkaContainer kafka = new KafkaContainer(image)) {
27+
kafka.start();
28+
29+
String bootstrap = kafka.getBootstrapServers();
30+
String topic = "hello";
31+
32+
Properties prodProps = new Properties();
33+
prodProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap);
34+
prodProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
35+
prodProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
36+
37+
try (KafkaProducer<String, String> producer = new KafkaProducer<>(prodProps)) {
38+
producer.send(new ProducerRecord<>(topic, "key", "hello")).get();
39+
} catch (Exception e) {
40+
throw new RuntimeException(e);
41+
}
42+
43+
Properties consProps = new Properties();
44+
consProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap);
45+
consProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group");
46+
consProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
47+
consProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
48+
consProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
49+
50+
try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consProps)) {
51+
consumer.subscribe(Collections.singletonList(topic));
52+
ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(10));
53+
ConsumerRecord<String, String> first = records.iterator().next();
54+
Assertions.assertEquals("hello", first.value());
55+
}
56+
}
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.testcontainers.podman;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.testcontainers.mysql.MySQLContainer;
6+
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
import java.sql.ResultSet;
10+
import java.sql.Statement;
11+
12+
class MysqlLiveTest {
13+
14+
@Test
15+
void whenQueryingDatabase_thenReturnsOne() throws Exception {
16+
17+
try (MySQLContainer mysql = new MySQLContainer("mysql:8.4")) {
18+
mysql.start();
19+
20+
try (Connection conn = DriverManager.getConnection(
21+
mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());
22+
Statement st = conn.createStatement()) {
23+
24+
st.execute("CREATE TABLE t(id INT PRIMARY KEY)");
25+
st.execute("INSERT INTO t VALUES (1)");
26+
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM t");
27+
rs.next();
28+
Assertions.assertEquals(1, rs.getInt(1));
29+
}
30+
}
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.testcontainers.podman;
2+
3+
4+
import com.redis.testcontainers.RedisContainer;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import redis.clients.jedis.Jedis;
8+
9+
class RedisLiveTest {
10+
11+
@Test
12+
void whenSettingValue_thenCanGetItBack() {
13+
try (RedisContainer redis = new RedisContainer("redis:7-alpine").withExposedPorts(6379)) {
14+
redis.start();
15+
16+
String host = redis.getHost();
17+
int port = redis.getFirstMappedPort();
18+
19+
try (Jedis jedis = new Jedis(host, port)) {
20+
jedis.set("greeting", "hello");
21+
String value = jedis.get("greeting");
22+
Assertions.assertEquals("hello", value);
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)