Skip to content

Commit 7836f16

Browse files
authored
Move Consul tests to JUnit Jupiter (#10732)
1 parent 8c8da7a commit 7836f16

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

modules/consul/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ description = "Testcontainers :: Consul"
33
dependencies {
44
api project(':testcontainers')
55

6+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.0'
7+
8+
testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
69
testImplementation 'com.ecwid.consul:consul-api:1.4.5'
710
testImplementation 'io.rest-assured:rest-assured:5.5.6'
811
testImplementation 'org.assertj:assertj-core:3.27.4'
912
}
13+
14+
test {
15+
useJUnitPlatform()
16+
}

modules/consul/src/test/java/org/testcontainers/consul/ConsulContainerTest.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import com.ecwid.consul.v1.Response;
55
import com.ecwid.consul.v1.kv.model.GetValue;
66
import io.restassured.RestAssured;
7-
import org.junit.ClassRule;
8-
import org.junit.Test;
7+
import org.junit.jupiter.api.AfterAll;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
910
import org.testcontainers.containers.GenericContainer;
1011

1112
import java.io.IOException;
@@ -16,25 +17,30 @@
1617

1718
import static org.assertj.core.api.Assertions.assertThat;
1819

19-
/**
20-
* This test shows the pattern to use the ConsulContainer @ClassRule for a junit test. It also has tests that ensure
21-
* the properties were added correctly by reading from Consul with the CLI and over HTTP.
22-
*/
23-
public class ConsulContainerTest {
20+
class ConsulContainerTest {
2421

25-
@ClassRule
26-
public static ConsulContainer consulContainer = new ConsulContainer("hashicorp/consul:1.15")
22+
private static ConsulContainer consul = new ConsulContainer("hashicorp/consul:1.15")
2723
.withConsulCommand("kv put config/testing1 value123");
2824

25+
@BeforeAll
26+
static void setup() {
27+
consul.start();
28+
}
29+
30+
@AfterAll
31+
static void teardown() {
32+
consul.stop();
33+
}
34+
2935
@Test
30-
public void readFirstPropertyPathWithCli() throws IOException, InterruptedException {
31-
GenericContainer.ExecResult result = consulContainer.execInContainer("consul", "kv", "get", "config/testing1");
36+
void readFirstPropertyPathWithCli() throws IOException, InterruptedException {
37+
GenericContainer.ExecResult result = consul.execInContainer("consul", "kv", "get", "config/testing1");
3238
final String output = result.getStdout().replaceAll("\\r?\\n", "");
3339
assertThat(output).contains("value123");
3440
}
3541

3642
@Test
37-
public void readFirstSecretPathOverHttpApi() {
43+
void readFirstSecretPathOverHttpApi() {
3844
io.restassured.response.Response response = RestAssured
3945
.given()
4046
.when()
@@ -46,11 +52,8 @@ public void readFirstSecretPathOverHttpApi() {
4652
}
4753

4854
@Test
49-
public void writeAndReadMultipleValuesUsingClient() {
50-
final ConsulClient consulClient = new ConsulClient(
51-
consulContainer.getHost(),
52-
consulContainer.getFirstMappedPort()
53-
);
55+
void writeAndReadMultipleValuesUsingClient() {
56+
final ConsulClient consulClient = new ConsulClient(consul.getHost(), consul.getFirstMappedPort());
5457

5558
final Map<String, String> properties = new HashMap<>();
5659
properties.put("value", "world");
@@ -70,6 +73,6 @@ public void writeAndReadMultipleValuesUsingClient() {
7073
}
7174

7275
private String getHostAndPort() {
73-
return consulContainer.getHost() + ":" + consulContainer.getMappedPort(8500);
76+
return consul.getHost() + ":" + consul.getMappedPort(8500);
7477
}
7578
}

0 commit comments

Comments
 (0)