application-test.properties

It is common to want to run all tests against H2 in memory database. A simple way to do this is to add a src/test/resources/application-test.properties to your project. In the application-test.properties you can effectively override the properties of the datasource and specify to use H2.

Run all tests against H2 in memory database.

When a Database instance is created automatically via DB.getDefault() then Ebean will look for the existence of application-test.properties in the classpath. If found this then typically specifies to use an in memory H2 datasource.

Example application-test.properties

## Create DB from scratch prior to running tests
ebean.db.ddl.generate=true
ebean.db.ddl.run=true

## Use H2 when running tests
datasource.db.username=sa
datasource.db.password=
datasource.db.databaseUrl=jdbc:h2:mem:tests
datasource.db.databaseDriver=org.h2.Driver

Programmatic - loadTestProperties()

When programmatically creating a Database instance using DatabaseConfig and DatabaseFactory there is a method databaseConfig.loadTestProperties() which will similarly look for a application-test.properties file if present.

@Override
public Database getObject() throws Exception {

  DatabaseConfig config = new DatabaseConfig();
  config.setName("db");
  config.loadFromProperties(properties);
  ...

  // load application-test.properties if present for running tests
  // typically using H2 in memory database
  config.loadTestProperties();

  // set as default and register so that Model can be
  // used if desired for save() and update() etc
  config.setDefaultServer(true);
  config.setRegister(true);

  return DatabaseFactory.create(config);
}