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:
urlissqlite::memory:anddriverissqlite3: the database lives as long as its connection;pool.max-sizeis 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 thesqlite3package bundles has no shared cache, which would let connections share one).pool.idle-timeoutandpool.max-lifetimeare 0 (never),pool.min-idleis 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 aPropagation.requiresNewtransaction waits for it and fails after the connection timeout;data.jdbc.schemaiscreate(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;
hasSqliteDriversays whether the application has thesqlite3driver (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< definitions) → boolObject> > -
Whether
definitionshold a SQLite driver: the bean the generator writes for thesqlite3package (sqlite3JdbcDriver), or one of the application's own of classSqliteJdbcDriver.
Constants
- propertySourceName → const String
- The name of the property source the replacement adds.
- url → const String
- The URL of every replaced database.