From c4d62a47da3c3543b958ee0310670cc7fe9b20d9 Mon Sep 17 00:00:00 2001 From: Bahtiyar Kaba Date: Thu, 17 May 2018 22:56:58 +0300 Subject: [PATCH] add test-containers module --- testing-modules/test-containers/README.md | 2 + testing-modules/test-containers/pom.xml | 109 ++++++++++++++++++ .../testconainers/GenericContainerTests.java | 45 ++++++++ .../PostgreSqlContainerTests.java | 36 ++++++ 4 files changed, 192 insertions(+) create mode 100644 testing-modules/test-containers/README.md create mode 100644 testing-modules/test-containers/pom.xml create mode 100644 testing-modules/test-containers/src/test/java/com/baeldung/testconainers/GenericContainerTests.java create mode 100644 testing-modules/test-containers/src/test/java/com/baeldung/testconainers/PostgreSqlContainerTests.java diff --git a/testing-modules/test-containers/README.md b/testing-modules/test-containers/README.md new file mode 100644 index 0000000000..160893581d --- /dev/null +++ b/testing-modules/test-containers/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Docker Test Containers in Java Tests](TODO link to be added.) diff --git a/testing-modules/test-containers/pom.xml b/testing-modules/test-containers/pom.xml new file mode 100644 index 0000000000..bb426675e5 --- /dev/null +++ b/testing-modules/test-containers/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + + + test-containers + 1.0-SNAPSHOT + + test-containers + Intro to Java Test Containers + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + + + src/test/resources + true + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.baeldung.TestLauncher + + + + + + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + + + org.testcontainers + testcontainers + 1.7.2 + + + org.testcontainers + postgresql + 1.7.2 + + + org.postgresql + postgresql + 42.2.2 + + + + + + UTF-8 + 1.8 + 5.1.0 + 1.0.1 + 4.12.1 + 2.8.2 + 1.4.196 + 2.11.0 + + 3.7.0 + 2.19.1 + 5.0.1.RELEASE + + + diff --git a/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/GenericContainerTests.java b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/GenericContainerTests.java new file mode 100644 index 0000000000..132fca5ad3 --- /dev/null +++ b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/GenericContainerTests.java @@ -0,0 +1,45 @@ +package com.baeldung.testconainers; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.platform.commons.annotation.Testable; +import org.testcontainers.containers.GenericContainer; + +@Testable +public class GenericContainerTests { + @ClassRule + public static GenericContainer simpleWebServer = new GenericContainer("alpine:3.2") + .withExposedPorts(80) + .withCommand("/bin/sh", "-c", "while true; do echo " + + "\"HTTP/1.1 200 OK\n\nHello World!\" | nc -l -p 80; done"); + + @Test + public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse() throws Exception { + String address = "http://" + simpleWebServer.getContainerIpAddress() + ":" + simpleWebServer.getMappedPort(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(); + } +} diff --git a/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/PostgreSqlContainerTests.java b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/PostgreSqlContainerTests.java new file mode 100644 index 0000000000..4261fa2353 --- /dev/null +++ b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/PostgreSqlContainerTests.java @@ -0,0 +1,36 @@ +package com.baeldung.testconainers; + +import static org.junit.Assert.assertEquals; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.platform.commons.annotation.Testable; +import org.testcontainers.containers.PostgreSQLContainer; + +@Testable +public class PostgreSqlContainerTests { + @Rule + public PostgreSQLContainer postgresContainer = new PostgreSQLContainer(); + + @Test + public void whenSelectQueryExecuted_thenResulstsReturned() throws Exception { + ResultSet resultSet = performQuery(postgresContainer, "SELECT 1"); + resultSet.next(); + int result = resultSet.getInt(1); + assertEquals(1, result); + } + + private ResultSet performQuery(PostgreSQLContainer postgres, String query) throws SQLException { + String jdbcUrl = postgres.getJdbcUrl(); + String username = postgres.getUsername(); + String password = postgres.getPassword(); + Connection conn = DriverManager.getConnection(jdbcUrl, username, password); + return conn.createStatement() + .executeQuery(query); + } +}