Skip to content

Commit 4931f31

Browse files
bahtiyarkabaCalamarBicefalo
authored andcommitted
Added docker compose tests for testcontainers module
1 parent 8f3f5cd commit 4931f31

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
simpleWebServer:
2+
image: alpine:3.2
3+
command: ["/bin/sh", "-c", "while true; do echo 'HTTP/1.1 200 OK\n\nHello World!' | nc -l -p 80; done"]

0 commit comments

Comments
 (0)