GenericContainer JUnit

JUnit addition to TestContainers

License

License

MIT
Categories

Categories

JUnit Unit Testing Container
GroupId

GroupId

com.github.npetzall.testcontainers.junit
ArtifactId

ArtifactId

generic
Last Version

Last Version

1.10.1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

GenericContainer JUnit
JUnit addition to TestContainers

Download generic

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.npetzall.testcontainers.junit/generic/ -->
<dependency>
    <groupId>com.github.npetzall.testcontainers.junit</groupId>
    <artifactId>generic</artifactId>
    <version>1.10.1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.npetzall.testcontainers.junit/generic/
implementation 'com.github.npetzall.testcontainers.junit:generic:1.10.1.0.0'
// https://jarcasting.com/artifacts/com.github.npetzall.testcontainers.junit/generic/
implementation ("com.github.npetzall.testcontainers.junit:generic:1.10.1.0.0")
'com.github.npetzall.testcontainers.junit:generic:jar:1.10.1.0.0'
<dependency org="com.github.npetzall.testcontainers.junit" name="generic" rev="1.10.1.0.0">
  <artifact name="generic" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.npetzall.testcontainers.junit', module='generic', version='1.10.1.0.0')
)
libraryDependencies += "com.github.npetzall.testcontainers.junit" % "generic" % "1.10.1.0.0"
[com.github.npetzall.testcontainers.junit/generic "1.10.1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.testcontainers : testcontainers jar 1.10.1
junit : junit jar 4.12
org.slf4j : slf4j-api jar 1.7.25

test (4)

Group / Artifact Type Version
org.testcontainers : mysql jar 1.10.1
ch.qos.logback : logback-classic jar 1.2.3
org.assertj : assertj-core jar 3.8.0
org.sonarsource.java : sonar-jacoco-listeners jar 4.14.0.11784

Project Modules

There are no modules declared in this project.

Build Status GitHub license Quality Gate Quality Gate Quality Gate GitHub tag Maven Central

JUnit Additions for testcontainers.

This is used instead of the Container as @Rule or @ClassRule

This was developed since it currently lacks the ability to assume that docker exists.

Depending on the type of test one is performing. The lack of docker should be an ignored test instead of a failing test.
This is to make a Failure speak about the SUT instead of your ability to run docker.

You want your tests to be small and keep the reasons for failure to a minimum.

Testcontainers

Examples GenericContainer or subclass:

import org.junit.ClassRule;
import org.junit.Test;
import com.github.npetzall.testcontainers.junit.generic.GenericContainerRule;
import java.sql.Connection;
import java.sql.Statement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.testcontainers.containers.MySQLContainer;
  
public class SomeTestClassWithGenericContainerRule {  
  
    @ClassRule
    public static GenericContainerRule<MySQLContainer> mysqlContainerRule = new GenericContainerRule<>(() -> new MySQLContainer())
                                  .assumeDockerIsPresent()
                                  .withAssumptions(container -> {
                                      try {
                                          container.getJdbcDriverInstance();
                                      } catch (Exception e) {
                                          throw new AssumptionViolatedException("No driver");
                                      }
                                  });
    
    @Test
    public void doSomeTest(){
        Connection connection = mysqlContainerRule.getContainer().createConnetion("");
        Statement statment = connection.createStatment();
        //do stuff with the connection
    }
      
    @Test
    public void doSomeOtherTest(){
        MysqlContainer mysqlContainer = mysqlContainerRule.getContainer();
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(mysqlContainer.getJdbcUrl());
        hikariConfig.setUsername(mysqlContainer.getUsername());
        hikariConfig.setPassword(mysqlContainer.getPassword());
    
        HikariDataSource ds = new HikariDataSource(hikariConfig);
        // do stuff with the datasource
    }
}

Example JdcbDatabaseContainer or subclass

import org.junit.ClassRule;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Statement;
import org.testcontainers.containers.MySQLContainer;
import com.github.npetzall.testcontainers.junit.jdbc.JdbcContainerRule;
  
import static com.github.npetzall.testcontainers.junit.jdbc.JdbcAssumptions.assumeDriverIsPresent;
  
public class SomeTestClassWithJdbcContainerRule {
    
    @ClassRule
    public static JdbcContainerRule<MySQLContainer> mysqlContainerJdbcRule = new JdbcContainerRule<>(() -> new MySQLContainer())
            .withInitScript("org/testcontainers/junit/jdbc/initscripts/mysqlInitScript.sql")
            .assumeDockerIsPresent()
            .withAssumptions(assumeDriverIsPresent())
            .withInitFunctions(connection -> {
                try {
                    Statement statement = connection.createStatement();
                    statement.execute("INSERT INTO junit_jdbc values (2, 'added by function');");
                    if (!connection.getAutoCommit()) {
                        connection.commit();
                    }
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
    
    @Test
    public void someTest() {
        MysqlContainer mysqlContainer = mysqlContainerJdbcRule.getContainer();
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(mysqlContainer.getJdbcUrl());
        hikariConfig.setUsername(mysqlContainer.getUsername());
        hikariConfig.setPassword(mysqlContainer.getPassword());
    
        HikariDataSource ds = new HikariDataSource(hikariConfig);
        // do stuff with the datasource
    }
}

Versions

Version
1.10.1.0.0
0.1.0
0.0.4