44import com .ecwid .consul .v1 .Response ;
55import com .ecwid .consul .v1 .kv .model .GetValue ;
66import 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 ;
910import org .testcontainers .containers .GenericContainer ;
1011
1112import java .io .IOException ;
1617
1718import 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