|
| 1 | +package com.baeldung.testconainers; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.File; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.net.HttpURLConnection; |
| 9 | +import java.net.URL; |
| 10 | + |
| 11 | +import org.junit.ClassRule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.testcontainers.containers.DockerComposeContainer; |
| 14 | + |
| 15 | +public class DockerComposeContainerTests { |
| 16 | +@ClassRule |
| 17 | +public static DockerComposeContainer compose = new DockerComposeContainer(new File("src/test/resources/test-compose.yml")) |
| 18 | + .withExposedService("simpleWebServer_1", 80); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void when() throws Exception { |
| 22 | + String address ="http://" + compose.getServiceHost("simpleWebServer_1", 80)+ ":"+ compose.getServicePort("simpleWebServer_1", 80); |
| 23 | + String response = simpleGetRequest(address); |
| 24 | + assertEquals(response, "Hello World!"); |
| 25 | + } |
| 26 | + |
| 27 | + private String simpleGetRequest(String address) throws Exception { |
| 28 | + URL url = new URL(address); |
| 29 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 30 | + con.setRequestMethod("GET"); |
| 31 | + |
| 32 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
| 33 | + String inputLine; |
| 34 | + StringBuffer content = new StringBuffer(); |
| 35 | + while ((inputLine = in.readLine()) != null) { |
| 36 | + content.append(inputLine); |
| 37 | + } |
| 38 | + in.close(); |
| 39 | + |
| 40 | + return content.toString(); |
| 41 | + } |
| 42 | +} |
0 commit comments