renamed back to testing-modules, pulled together testing-modules-2 modules into single module
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Docker Test Containers in Java Tests](http://www.baeldung.com/docker-test-containers)
|
||||
@@ -0,0 +1,123 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId> -->
|
||||
<artifactId>test-containers</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>test-containers</name>
|
||||
<description>Intro to Java Test Containers</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit.vintage.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers</artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>postgresql </artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>selenium </artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-remote-driver</artifactId>
|
||||
<version>${selenium-remote-driver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-chrome-driver</artifactId>
|
||||
<version>${selenium-remote-driver.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/test/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.TestLauncher</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.5.0</junit.platform.version>
|
||||
<junit.vintage.version>5.5.0</junit.vintage.version>
|
||||
<log4j2.version>2.12.0</log4j2.version>
|
||||
<testcontainers.version>1.11.4</testcontainers.version>
|
||||
<postgresql.version>42.2.6</postgresql.version>
|
||||
<selenium-remote-driver.version>3.141.59</selenium-remote-driver.version>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-platform-surefire-provider.version>1.3.2</junit-platform-surefire-provider.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
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 DockerComposeContainerLiveTest {
|
||||
@ClassRule
|
||||
public static DockerComposeContainer compose =
|
||||
new DockerComposeContainer(
|
||||
new File("src/test/resources/test-compose.yml"))
|
||||
.withExposedService("simpleWebServer_1", 80);
|
||||
|
||||
@Test
|
||||
public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse()
|
||||
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();
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
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 GenericContainerLiveTest {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
+36
@@ -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 PostgreSqlContainerLiveTest {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.testconainers;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.testcontainers.containers.BrowserWebDriverContainer;
|
||||
|
||||
public class WebDriverContainerLiveTest {
|
||||
@Rule
|
||||
public BrowserWebDriverContainer chrome
|
||||
= new BrowserWebDriverContainer()
|
||||
.withCapabilities(new ChromeOptions());
|
||||
|
||||
@Test
|
||||
public void whenNavigatedToPage_thenHeadingIsInThePage() {
|
||||
RemoteWebDriver driver = chrome.getWebDriver();
|
||||
driver.get("http://example.com");
|
||||
String heading = driver.findElement(By.xpath("/html/body/div/h1"))
|
||||
.getText();
|
||||
assertEquals("Example Domain", heading);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"]
|
||||
Reference in New Issue
Block a user