Skip to main content

Database testing

Use SHAFT database actions to open a JDBC connection, execute statements, and attach query evidence to the test report.

Prerequisites​

  • A SHAFT Maven project. Install SHAFT walks through generating one.
  • The JDBC driver dependency for the database you connect to, on the test classpath. See connection strings and, for Oracle, the Oracle setup.
  • A reachable, disposable database or schema with read permission. Keep credentials outside source control.

See database actions, connection strings, and the Oracle setup.

First useful query test​

Start with a read-only query against a disposable schema, containerized database, or in-memory database. Keep credentials outside source control and validate a small result before adding write operations.

SHAFT.DB database = new SHAFT.DB("jdbc:h2:mem:test");
ResultSet rows = database.executeSelectQuery("SELECT 1");

SHAFT.Validations.assertThat().object(rows).isNotNull();

Run the test with Maven:

mvn test

The query, connection target, and validation result are attached to the test report. If the connection fails, verify the JDBC URL, driver dependency, network route, schema permissions, and whether CI has the required secret values.

Verify​

  • Maven reports the test as run and passed.
  • The report for the test shows the query, the connection target, and the validation result.
  • If the connection fails, work through the checks above in order: JDBC URL, driver dependency, network route, schema permissions, CI secrets.

Choose the next reference​

NeedStart with
Build a JDBC URLConnection Strings
Run selects, updates, or result validationDB Actions
Connect to OracleOracle JDBC Setup