Java - Test Containers for Oracle

About

Example on how to use OracleContainer Xe of Test container.

Usage

There is two usage:

  • via container instantation
  • or via a modification of the Jdbc url

Via OracleContainer instantiation

The OracleContainer instantiation can be scoped:

  • to a class (or a test)
  • to the run (singleton)

in a test class

@ClassRule
public static OracleContainer oracle = new OracleContainer("wnameless/oracle-xe-11g-r2")
	.withUsername("system")
	.withPassword("oracle")
	.withExposedPorts(1521)
	.withEnv("ORACLE_ALLOW_REMOTE", "true");

@BeforeClass
public static void setUp() {
  String url = oracle.getJdbcUrl();
  String username =oracle.getUsername();
  String password = oracle.getPassword();
  // Make your connection
}

a singleton

Doc

  • instantiate it as a static field
public abstract class OracleTestContainer {

  static final OracleContainer ORACLE_CONTAINER;

  static {
    ORACLE_CONTAINER = new OracleContainer("wnameless/oracle-xe-11g-r2")
      .withUsername("system")
      .withPassword("oracle")
      .withExposedPorts(1521)
      .withEnv("ORACLE_ALLOW_REMOTE", "true");
  }

}
  • and use it in the test
@Test
public static void setUp() {
  String url = ORACLE_CONTAINER.getJdbcUrl();
  String username =ORACLE_CONTAINER.getUsername();
  String password = ORACLE_CONTAINER.getPassword();
  // Make your connection
}

Via JDBC URL modification

In this case, you just modify the Jdbc Url and use your code as it. You do it in tow steps

  • Define the image in the test Container properties file
oracle.container.image=wnameless/oracle-xe-11g-r2
  • Modify the url in your code by adding tc and removing the host and port
@BeforeClass
public static void setUp() {
  String url = "jdbc:tc:oracle:thin:@///ORCL";
  String username = "system";
  String password = "oracle";
  // Make your connection
}

Powered by ComboStrap