Skip to content

Commit 1084b20

Browse files
Migrate examples to JUnit Jupiter (#7417)
Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
1 parent 0fc30f6 commit 1084b20

File tree

28 files changed

+150
-88
lines changed

28 files changed

+150
-88
lines changed

examples/hazelcast/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ dependencies {
1111
testImplementation 'com.hazelcast:hazelcast:5.2.3'
1212
testImplementation 'ch.qos.logback:logback-classic:1.3.8'
1313
testImplementation 'org.assertj:assertj-core:3.24.2'
14+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
15+
}
16+
17+
test {
18+
useJUnitPlatform()
1419
}

examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.hazelcast.client.HazelcastClient;
44
import com.hazelcast.client.config.ClientConfig;
55
import com.hazelcast.core.HazelcastInstance;
6-
import org.junit.After;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.Test;
88
import org.testcontainers.containers.GenericContainer;
99
import org.testcontainers.containers.Network;
1010
import org.testcontainers.containers.wait.strategy.Wait;
@@ -18,7 +18,7 @@
1818
/**
1919
* Examples with Hazelcast using both a single container and a cluster with two containers.
2020
*/
21-
public class HazelcastTest {
21+
class HazelcastTest {
2222

2323
// Hazelcast values
2424
private static final String HZ_IMAGE_NAME = "hazelcast/hazelcast:5.2.0";
@@ -46,13 +46,13 @@ public class HazelcastTest {
4646

4747
private static final String TRUE_VALUE = "true";
4848

49-
@After
50-
public void cleanUp() {
49+
@AfterEach
50+
void cleanUp() {
5151
HazelcastClient.shutdownAll();
5252
}
5353

5454
@Test
55-
public void singleHazelcastContainer() {
55+
void singleHazelcastContainer() {
5656
try (
5757
GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME))
5858
.withExposedPorts(DEFAULT_EXPOSED_PORT)
@@ -76,7 +76,7 @@ public void singleHazelcastContainer() {
7676
}
7777

7878
@Test
79-
public void hazelcastCluster() {
79+
void hazelcastCluster() {
8080
Network network = Network.newNetwork();
8181
try (
8282
GenericContainer<?> container1 = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME))

examples/immudb/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ repositories {
99
dependencies {
1010
implementation 'io.codenotary:immudb4j:1.0.0'
1111
testImplementation 'org.testcontainers:testcontainers'
12+
testImplementation 'org.testcontainers:junit-jupiter'
1213
testImplementation 'org.assertj:assertj-core:3.24.2'
1314
testImplementation 'com.google.guava:guava:23.0'
1415
testImplementation 'ch.qos.logback:logback-classic:1.3.8'
16+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
17+
}
18+
19+
test {
20+
useJUnitPlatform()
1521
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import io.codenotary.immudb4j.Entry;
22
import io.codenotary.immudb4j.ImmuClient;
33
import io.codenotary.immudb4j.exceptions.VerificationException;
4-
import org.junit.After;
5-
import org.junit.Assert;
6-
import org.junit.Before;
7-
import org.junit.ClassRule;
8-
import org.junit.Test;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
98
import org.testcontainers.containers.GenericContainer;
109
import org.testcontainers.containers.wait.strategy.Wait;
10+
import org.testcontainers.junit.jupiter.Container;
11+
import org.testcontainers.junit.jupiter.Testcontainers;
1112

1213
import static org.assertj.core.api.Assertions.assertThat;
1314

1415
/**
1516
* Test class for the ImmuDbClient.
1617
*/
17-
public class ImmuDbTest {
18+
@Testcontainers
19+
class ImmuDbTest {
1820

1921
// Default port for the ImmuDb server
2022
private static final int IMMUDB_PORT = 3322;
@@ -29,16 +31,16 @@ public class ImmuDbTest {
2931
private final String IMMUDB_DATABASE = "defaultdb";
3032

3133
// Test container for the ImmuDb database, with the latest version of the image and exposed port
32-
@ClassRule
34+
@Container
3335
public static final GenericContainer<?> immuDbContainer = new GenericContainer<>("codenotary/immudb:1.3")
3436
.withExposedPorts(IMMUDB_PORT)
3537
.waitingFor(Wait.forLogMessage(".*Web API server enabled.*", 1));
3638

3739
// ImmuClient used to interact with the DB
3840
private ImmuClient immuClient;
3941

40-
@Before
41-
public void setUp() {
42+
@BeforeEach
43+
void setUp() {
4244
this.immuClient =
4345
ImmuClient
4446
.newBuilder()
@@ -48,13 +50,13 @@ public void setUp() {
4850
this.immuClient.openSession(IMMUDB_DATABASE, IMMUDB_USER, IMMUDB_PASSWORD);
4951
}
5052

51-
@After
52-
public void tearDown() {
53+
@AfterEach
54+
void tearDown() {
5355
this.immuClient.closeSession();
5456
}
5557

5658
@Test
57-
public void testGetValue() {
59+
void testGetValue() {
5860
try {
5961
immuClient.set("test1", "test2".getBytes());
6062

@@ -64,10 +66,10 @@ public void testGetValue() {
6466
byte[] value = entry.getValue();
6567
assertThat(new String(value)).isEqualTo("test2");
6668
} else {
67-
Assert.fail();
69+
Assertions.fail();
6870
}
6971
} catch (VerificationException e) {
70-
Assert.fail();
72+
Assertions.fail();
7173
}
7274
}
7375
}

examples/kafka-cluster/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ dependencies {
1414
testImplementation 'org.assertj:assertj-core:3.24.2'
1515
testImplementation 'com.google.guava:guava:23.0'
1616
testImplementation 'ch.qos.logback:logback-classic:1.3.8'
17+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
18+
}
19+
20+
test {
21+
useJUnitPlatform()
1722
}

examples/kafka-cluster/src/test/java/com/example/kafkacluster/KafkaContainerClusterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.apache.kafka.clients.producer.ProducerRecord;
1414
import org.apache.kafka.common.serialization.StringDeserializer;
1515
import org.apache.kafka.common.serialization.StringSerializer;
16-
import org.junit.Test;
16+
import org.junit.jupiter.api.Test;
1717
import org.rnorth.ducttape.unreliables.Unreliables;
1818

1919
import java.time.Duration;
@@ -25,10 +25,10 @@
2525
import static org.assertj.core.api.Assertions.assertThat;
2626
import static org.assertj.core.api.Assertions.tuple;
2727

28-
public class KafkaContainerClusterTest {
28+
class KafkaContainerClusterTest {
2929

3030
@Test
31-
public void testKafkaContainerCluster() throws Exception {
31+
void testKafkaContainerCluster() throws Exception {
3232
try (KafkaContainerCluster cluster = new KafkaContainerCluster("6.2.1", 3, 2)) {
3333
cluster.start();
3434
String bootstrapServers = cluster.getBootstrapServers();
@@ -40,7 +40,7 @@ public void testKafkaContainerCluster() throws Exception {
4040
}
4141

4242
@Test
43-
public void testKafkaContainerKraftCluster() throws Exception {
43+
void testKafkaContainerKraftCluster() throws Exception {
4444
try (KafkaContainerKraftCluster cluster = new KafkaContainerKraftCluster("7.0.0", 3, 2)) {
4545
cluster.start();
4646
String bootstrapServers = cluster.getBootstrapServers();
@@ -52,7 +52,7 @@ public void testKafkaContainerKraftCluster() throws Exception {
5252
}
5353

5454
@Test
55-
public void testKafkaContainerKraftClusterAfterConfluentPlatform740() throws Exception {
55+
void testKafkaContainerKraftClusterAfterConfluentPlatform740() throws Exception {
5656
try (KafkaContainerKraftCluster cluster = new KafkaContainerKraftCluster("7.4.0", 3, 2)) {
5757
cluster.start();
5858
String bootstrapServers = cluster.getBootstrapServers();

examples/nats/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ dependencies {
1212
testImplementation 'io.nats:jnats:2.16.13'
1313
testImplementation 'ch.qos.logback:logback-classic:1.3.8'
1414
testImplementation 'org.apache.httpcomponents:httpclient:4.5.14'
15+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
16+
}
17+
18+
test {
19+
useJUnitPlatform()
1520
}

examples/nats/src/test/java/com/example/NatsContainerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
import org.apache.http.client.methods.HttpGet;
99
import org.apache.http.client.methods.HttpUriRequest;
1010
import org.apache.http.impl.client.HttpClientBuilder;
11-
import org.junit.Test;
11+
import org.junit.jupiter.api.Test;
1212
import org.testcontainers.containers.GenericContainer;
1313

1414
import java.io.IOException;
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
1717

18-
public class NatsContainerTest {
18+
class NatsContainerTest {
1919

2020
public static final Integer NATS_PORT = 4222;
2121

2222
public static final Integer NATS_MGMT_PORT = 8222;
2323

2424
@Test
25-
public void test() throws IOException, InterruptedException {
25+
void test() throws IOException, InterruptedException {
2626
try (
2727
GenericContainer<?> nats = new GenericContainer<>("nats:2.9.8-alpine3.16")
2828
.withExposedPorts(NATS_PORT, NATS_MGMT_PORT)
@@ -38,7 +38,7 @@ public void test() throws IOException, InterruptedException {
3838
}
3939

4040
@Test
41-
public void testServerStatus() throws IOException {
41+
void testServerStatus() throws IOException {
4242
try (
4343
GenericContainer<?> nats = new GenericContainer<>("nats:2.9.8-alpine3.16")
4444
.withExposedPorts(NATS_PORT, NATS_MGMT_PORT)

examples/redis-backed-cache/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ dependencies {
1212
implementation 'com.google.code.gson:gson:2.10.1'
1313
implementation 'com.google.guava:guava:23.0'
1414
testImplementation 'org.testcontainers:testcontainers'
15-
testImplementation 'junit:junit:4.13.2'
15+
testImplementation 'org.testcontainers:junit-jupiter'
16+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
1617
testImplementation 'ch.qos.logback:logback-classic:1.3.8'
1718
testImplementation 'org.assertj:assertj-core:3.24.2'
1819
}
20+
21+
test {
22+
useJUnitPlatform()
23+
}

examples/redis-backed-cache/src/test/java/RedisBackedCacheTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import com.mycompany.cache.Cache;
22
import com.mycompany.cache.RedisBackedCache;
3-
import org.junit.Before;
4-
import org.junit.Rule;
5-
import org.junit.Test;
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
65
import org.testcontainers.containers.GenericContainer;
6+
import org.testcontainers.junit.jupiter.Container;
7+
import org.testcontainers.junit.jupiter.Testcontainers;
78
import org.testcontainers.utility.DockerImageName;
89
import redis.clients.jedis.Jedis;
910

@@ -14,23 +15,24 @@
1415
/**
1516
* Integration test for Redis-backed cache implementation.
1617
*/
17-
public class RedisBackedCacheTest {
18+
@Testcontainers
19+
class RedisBackedCacheTest {
1820

19-
@Rule
21+
@Container
2022
public GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:3.0.6"))
2123
.withExposedPorts(6379);
2224

2325
private Cache cache;
2426

25-
@Before
26-
public void setUp() throws Exception {
27+
@BeforeEach
28+
void setUp() throws Exception {
2729
Jedis jedis = new Jedis(redis.getHost(), redis.getMappedPort(6379));
2830

2931
cache = new RedisBackedCache(jedis, "test");
3032
}
3133

3234
@Test
33-
public void testFindingAnInsertedValue() {
35+
void testFindingAnInsertedValue() {
3436
cache.put("foo", "FOO");
3537
Optional<String> foundObject = cache.get("foo", String.class);
3638

@@ -41,7 +43,7 @@ public void testFindingAnInsertedValue() {
4143
}
4244

4345
@Test
44-
public void testNotFindingAValueThatWasNotInserted() {
46+
void testNotFindingAValueThatWasNotInserted() {
4547
Optional<String> foundObject = cache.get("bar", String.class);
4648

4749
assertThat(foundObject.isPresent())

0 commit comments

Comments
 (0)