Connection to the database
To make assertions on a database, it is necessary to connect. The concept of AssertDbConnection represent how
AssertJ-DB can retrieve data and schema information from database.
It’s also with a AssertDbConnection that you can instantiate the following element : Table, Request, Changes.
AssertDbConnection
A AssertDbConnection is created with the factory AssertDbConnectionFactory.
There are 2 way to begin the AssertDbConnectionFactory, with a DataSource ( the classic Java way to get a Connection to a database ) or with JDBC connection information.
Below is an example of using a DataSource to connect to H2 in memory database :
AssertDbConnection connection = AssertDbConnectionFactory.of("jdbc:h2:mem:test", "sa", "").create();
And using a DataSource to connect :
AssertDbConnection connection = AssertDbConnectionFactory.of(dataSource).create();
LetterCase setup
AssertDbConnectionFactory provide the capacity to indicate the LetterCase to use for the tables, columns and primary keys.
AssertDbConnection connection = AssertDbConnectionFactory
.of(dataSource)
.letterCase(tableLetterCase, columnLetterCase, pkLetterCase)
.create();
For more information, see the paragraph on LetterCase.
Schema retrieval mode
For many assertions, AssertJ-DB require to discover database schema metadata ( list of tables, columns, … ). When the schema contains many tables this operation can slow down the tests executions.
To avoid that and when the database schema is not updated during the test session, you can use the option SchemaMetaDataMode of AssertDbConnectionFactory to keep in memory the schema.
Available mode are :
-
DYNAMIC ( default ): Retrieve schema metadata each time is required.
-
STATIC : Retrieve schema metadata only once and keep in memory for all duration of connection.
Below is an example of using the STATIC mode for a connection :
AssertDbConnection connection = AssertDbConnectionFactory.of(dataSource)
.schemaMetaDataMode(SchemaMetaDataMode.STATIC)
.create();