TestDatabase class final

Replaces the SQL databases of the properties by in-memory SQLite ones before the context starts, as @DataTest does: each spring.datasource.url and spring.datasources.<name>.url is set to sqlite::memory:.

What it sets for each database, as a property source that wins over the others:

  • url is sqlite::memory: and driver is sqlite3: the database lives as long as its connection;
  • pool.max-size is 1, because each connection of SQLite gets a database of its own, so a second connection would see none of the tables (the SQLite that the sqlite3 package bundles has no shared cache, which would let connections share one). pool.idle-timeout and pool.max-lifetime are 0 (never), pool.min-idle is 0, so the pool never closes the connection and loses the data. The connection is used by one task at a time, which a test running in its transaction is, but a Propagation.requiresNew transaction waits for it and fails after the connection timeout;
  • data.jdbc.schema is create (the tables are made from the entities) when the property is not set and neither migrations nor SQL scripts make the schema: with them the application's own schema is used, as it is in production.

Every other property stays: the username, the migrations' settings, the logging of SQL. A database that is not configured is left alone, and the application's own DataSource bean (one the properties do not describe) cannot be replaced.

The start fails with a StateError when a database is to be replaced and the application has no SQLite driver, since the tests would otherwise use the configured database (Spring fails the same way when there is no embedded database).

Spring (Java): @AutoConfigureTestDatabase (TestDatabaseAutoConfiguration replaces the DataSource with an embedded H2, HSQL or Derby database).

Implemented types

Constructors

TestDatabase({required bool hasSqliteDriver})
A replacement; hasSqliteDriver says whether the application has the sqlite3 driver (see hasSqliteDriverIn).
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
hasSqliteDriver → bool
Whether the application has a SQLite driver to open the databases.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

initialize(ConfigurableApplicationContext context) → void
Prepares context, which has its environment and bean definitions but no beans yet.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

hasSqliteDriverIn(Iterable<BeanDefinition<Object>> definitions) → bool
Whether definitions hold a SQLite driver: the bean the generator writes for the sqlite3 package (sqlite3JdbcDriver), or one of the application's own of class SqliteJdbcDriver.

Constants

propertySourceName → const String
The name of the property source the replacement adds.
url → const String
The URL of every replaced database.