Class Jdbi3Extension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

    public class Jdbi3Extension
    extends Object
    implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
    A JUnit Jupiter Extension to easily test JDBI 3 DAOs using the "Fluent API" (as opposed to the SQL Objects API) against any database and using transaction rollback to make sure tests never commit to the database.

    You must supply one of three methods for obtaining a database Connection: (1) a DataSource, (2) a JDBI ConnectionFactory, or (3) the JDBC URL, username, and password.

    Before each tests, sets up a transaction. After each test completes, rolls the transaction back.

    NOTE: If the DAO under test creates its own Handles, then you need to make sure that you always use the same connection otherwise there will be transaction isolation issues. Prefer using the Handle provided by this extension.

    • Method Detail

      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)
        Opens a Handle and begins a transaction.
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        Parameters:
        context - the extension context
        See Also:
        Jdbi.open(), Handle.begin()
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
        Rolls back the transaction and closes the Handle.
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
        Parameters:
        context - the extension context
        See Also:
        Handle.rollback(), Handle.close()