Table of Contents

Java - Test Containers for Oracle

About

Example on how to use OracleContainer Xe of Test container.

Usage

There is two usage:

Via OracleContainer instantiation

The OracleContainer instantiation can be scoped:

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

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");
  }

}
@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

oracle.container.image=wnameless/oracle-xe-11g-r2
@BeforeClass
public static void setUp() {
  String url = "jdbc:tc:oracle:thin:@///ORCL";
  String username = "system";
  String password = "oracle";
  // Make your connection
}