Skip to content

Commit c4d62a4

Browse files
bahtiyarkabaCalamarBicefalo
authored andcommitted
add test-containers module
1 parent eac1e3c commit c4d62a4

4 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [Docker Test Containers in Java Tests](TODO link to be added.)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId> -->
7+
<artifactId>test-containers</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<name>test-containers</name>
11+
<description>Intro to Java Test Containers</description>
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>parent-modules</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
<relativePath>../../</relativePath>
18+
</parent>
19+
20+
21+
<build>
22+
<resources>
23+
<resource>
24+
<directory>src/test/resources</directory>
25+
<filtering>true</filtering>
26+
</resource>
27+
</resources>
28+
<plugins>
29+
<plugin>
30+
<artifactId>maven-surefire-plugin</artifactId>
31+
<version>${maven-surefire-plugin.version}</version>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.junit.platform</groupId>
35+
<artifactId>junit-platform-surefire-provider</artifactId>
36+
<version>${junit.platform.version}</version>
37+
</dependency>
38+
</dependencies>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.codehaus.mojo</groupId>
42+
<artifactId>exec-maven-plugin</artifactId>
43+
<version>1.6.0</version>
44+
<executions>
45+
<execution>
46+
<goals>
47+
<goal>java</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
<configuration>
52+
<mainClass>com.baeldung.TestLauncher</mainClass>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
<dependencies>
59+
<dependency>
60+
<groupId>org.junit.platform</groupId>
61+
<artifactId>junit-platform-runner</artifactId>
62+
<version>${junit.platform.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.junit.vintage</groupId>
67+
<artifactId>junit-vintage-engine</artifactId>
68+
<version>${junit.vintage.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.logging.log4j</groupId>
73+
<artifactId>log4j-core</artifactId>
74+
<version>${log4j2.version}</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.testcontainers</groupId>
78+
<artifactId>testcontainers</artifactId>
79+
<version>1.7.2</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.testcontainers</groupId>
83+
<artifactId>postgresql </artifactId>
84+
<version>1.7.2</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.postgresql</groupId>
88+
<artifactId>postgresql</artifactId>
89+
<version>42.2.2</version>
90+
</dependency>
91+
92+
</dependencies>
93+
94+
<properties>
95+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
96+
<java.version>1.8</java.version>
97+
<junit.jupiter.version>5.1.0</junit.jupiter.version>
98+
<junit.platform.version>1.0.1</junit.platform.version>
99+
<junit.vintage.version>4.12.1</junit.vintage.version>
100+
<log4j2.version>2.8.2</log4j2.version>
101+
<h2.version>1.4.196</h2.version>
102+
<mockito.version>2.11.0</mockito.version>
103+
104+
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
105+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
106+
<spring.version>5.0.1.RELEASE</spring.version>
107+
</properties>
108+
109+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.testconainers;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.BufferedReader;
6+
import java.io.InputStreamReader;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
10+
import org.junit.ClassRule;
11+
import org.junit.Test;
12+
import org.junit.platform.commons.annotation.Testable;
13+
import org.testcontainers.containers.GenericContainer;
14+
15+
@Testable
16+
public class GenericContainerTests {
17+
@ClassRule
18+
public static GenericContainer simpleWebServer = new GenericContainer("alpine:3.2")
19+
.withExposedPorts(80)
20+
.withCommand("/bin/sh", "-c", "while true; do echo "
21+
+ "\"HTTP/1.1 200 OK\n\nHello World!\" | nc -l -p 80; done");
22+
23+
@Test
24+
public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse() throws Exception {
25+
String address = "http://" + simpleWebServer.getContainerIpAddress() + ":" + simpleWebServer.getMappedPort(80);
26+
String response = simpleGetRequest(address);
27+
assertEquals(response, "Hello World!");
28+
}
29+
30+
private String simpleGetRequest(String address) throws Exception {
31+
URL url = new URL(address);
32+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
33+
con.setRequestMethod("GET");
34+
35+
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
36+
String inputLine;
37+
StringBuffer content = new StringBuffer();
38+
while ((inputLine = in.readLine()) != null) {
39+
content.append(inputLine);
40+
}
41+
in.close();
42+
43+
return content.toString();
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.testconainers;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.platform.commons.annotation.Testable;
13+
import org.testcontainers.containers.PostgreSQLContainer;
14+
15+
@Testable
16+
public class PostgreSqlContainerTests {
17+
@Rule
18+
public PostgreSQLContainer postgresContainer = new PostgreSQLContainer();
19+
20+
@Test
21+
public void whenSelectQueryExecuted_thenResulstsReturned() throws Exception {
22+
ResultSet resultSet = performQuery(postgresContainer, "SELECT 1");
23+
resultSet.next();
24+
int result = resultSet.getInt(1);
25+
assertEquals(1, result);
26+
}
27+
28+
private ResultSet performQuery(PostgreSQLContainer postgres, String query) throws SQLException {
29+
String jdbcUrl = postgres.getJdbcUrl();
30+
String username = postgres.getUsername();
31+
String password = postgres.getPassword();
32+
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
33+
return conn.createStatement()
34+
.executeQuery(query);
35+
}
36+
}

0 commit comments

Comments
 (0)