11package org .testcontainers .junit ;
22
33import lombok .Cleanup ;
4- import org .junit .BeforeClass ;
5- import org .junit .Rule ;
6- import org .junit .Test ;
4+ import org .junit .jupiter .api .BeforeAll ;
5+ import org .junit .jupiter .api .Test ;
76import org .testcontainers .containers .NginxContainer ;
87import org .testcontainers .containers .wait .strategy .HttpWaitStrategy ;
98import org .testcontainers .utility .DockerImageName ;
2019
2120import static org .assertj .core .api .Assertions .assertThat ;
2221
23- public class SimpleNginxTest {
22+ class SimpleNginxTest {
2423
2524 private static final DockerImageName NGINX_IMAGE = DockerImageName .parse ("nginx:1.27.0-alpine3.19-slim" );
2625
2726 private static String tmpDirectory = System .getProperty ("user.home" ) + "/.tmp-test-container" ;
2827
29- // creatingContainer {
30- @ Rule
31- public NginxContainer nginx = new NginxContainer (NGINX_IMAGE )
32- .withCopyFileToContainer (MountableFile .forHostPath (tmpDirectory ), "/usr/share/nginx/html" )
33- .waitingFor (new HttpWaitStrategy ());
34-
35- // }
36-
3728 @ SuppressWarnings ({ "Duplicates" , "ResultOfMethodCallIgnored" })
38- @ BeforeClass
39- public static void setupContent () throws Exception {
29+ @ BeforeAll
30+ static void setupContent () throws Exception {
4031 // addCustomContent {
4132 // Create a temporary dir
4233 File contentFolder = new File (tmpDirectory );
@@ -53,15 +44,24 @@ public static void setupContent() throws Exception {
5344 }
5445
5546 @ Test
56- public void testSimple () throws Exception {
57- // getFromNginxServer {
58- URL baseUrl = nginx .getBaseUrl ("http" , 80 );
47+ void testSimple () throws Exception {
48+ try (
49+ // creatingContainer {
50+ NginxContainer nginx = new NginxContainer (NGINX_IMAGE )
51+ .withCopyFileToContainer (MountableFile .forHostPath (tmpDirectory ), "/usr/share/nginx/html" )
52+ .waitingFor (new HttpWaitStrategy ());
53+ // }
54+ ) {
55+ nginx .start ();
56+ // getFromNginxServer {
57+ URL baseUrl = nginx .getBaseUrl ("http" , 80 );
5958
60- assertThat (responseFromNginx (baseUrl ))
61- .as ("An HTTP GET from the Nginx server returns the index.html from the custom content directory" )
62- .contains ("Hello World!" );
63- // }
64- assertHasCorrectExposedAndLivenessCheckPorts (nginx );
59+ assertThat (responseFromNginx (baseUrl ))
60+ .as ("An HTTP GET from the Nginx server returns the index.html from the custom content directory" )
61+ .contains ("Hello World!" );
62+ // }
63+ assertHasCorrectExposedAndLivenessCheckPorts (nginx );
64+ }
6565 }
6666
6767 private void assertHasCorrectExposedAndLivenessCheckPorts (NginxContainer nginxContainer ) {
0 commit comments