Added docker compose tests for testcontainers module
This commit is contained in:
parent
8f3f5cdec1
commit
4931f31d91
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.testconainers;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.testcontainers.containers.DockerComposeContainer;
|
||||
|
||||
public class DockerComposeContainerTests {
|
||||
@ClassRule
|
||||
public static DockerComposeContainer compose = new DockerComposeContainer(new File("src/test/resources/test-compose.yml"))
|
||||
.withExposedService("simpleWebServer_1", 80);
|
||||
|
||||
@Test
|
||||
public void when() throws Exception {
|
||||
String address ="http://" + compose.getServiceHost("simpleWebServer_1", 80)+ ":"+ compose.getServicePort("simpleWebServer_1", 80);
|
||||
String response = simpleGetRequest(address);
|
||||
assertEquals(response, "Hello World!");
|
||||
}
|
||||
|
||||
private String simpleGetRequest(String address) throws Exception {
|
||||
URL url = new URL(address);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
return content.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
simpleWebServer:
|
||||
image: alpine:3.2
|
||||
command: ["/bin/sh", "-c", "while true; do echo 'HTTP/1.1 200 OK\n\nHello World!' | nc -l -p 80; done"]
|
Loading…
Reference in New Issue