Class JdbiExtension
- All Implemented Interfaces:
AfterAllCallback,AfterEachCallback,BeforeAllCallback,BeforeEachCallback,Extension,ParameterResolver
- Direct Known Subclasses:
JdbiExternalPostgresExtension,JdbiGenericExtension,JdbiH2Extension,JdbiOtjPostgresExtension,JdbiPostgresExtension,JdbiSqliteExtension,JdbiTestcontainersExtension
Subclasses can be used with the @ExtendWith annotation to declare an extension if they provide a public no-args constructor.
When using declarative registration, test methods can declare a Jdbi and/or a Handle parameter which is injected through this extension. The
getJdbi() is used to obtain the Jdbi object and getSharedHandle()} is used for the Handle instance.
Programmatic registration is preferred as this allows further customization of each extension.
-
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(ExtensionContext context) voidafterEach(ExtensionContext context) final <T> TConvenience method to attach an extension (such as a SqlObject) to the shared handle.voidbeforeAll(ExtensionContext context) voidbeforeEach(ExtensionContext context) final JdbiExtensionenableLeakChecker(boolean enable) Enable tracking of cleanable resources and handles when running tests.static JdbiExtensionCreates an extension that uses an external (outside the scope of an unit test class) postgres database.final JdbigetJdbi()Returns aJdbiinstance linked to the data source used by this extension.final HandleReturns a sharedHandleused by the extension.abstract StringgetUrl()Returns a JDBC url representing the data source used by this extension.static JdbiExtensionh2()Creates a new extension using the H2 database.final JdbiExtensionWhen creating theJdbiinstance, call theJdbi.installPlugins()method, which loads all plugins discovered by theServiceLoaderAPI.final HandleOpen a newHandleto the used data source.static JdbiOtjPostgresExtensionCreates a new extension using a managed, embedded postgres database.static JdbiExtensionpostgres(de.softwareforge.testing.postgres.junit5.EmbeddedPgExtension pg) Creates a new extension using a managed, embedded postgres database.resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) static JdbiExtensionsqlite()Creates a new extension using the SQLite database.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) final <C extends JdbiConfig<C>>
JdbiExtensionwithConfig(Class<C> configClass, Consumer<C> configurer) Set aJdbiConfigparameter when creating theJdbiinstance.final JdbiExtensionwithInitializer(JdbiExtensionInitializer initializer) Sets aJdbiExtensionInitializerto initialize theJdbiinstance or the attached data source before running a test.final JdbiExtensionwithPlugin(JdbiPlugin plugin) Install aJdbiPluginwhen creating theJdbiinstance.final JdbiExtensionwithPlugins(JdbiPlugin... pluginList) Install multipleJdbiPlugins when creating theJdbiinstance.
-
Method Details
-
postgres
public static JdbiExtension postgres(de.softwareforge.testing.postgres.junit5.EmbeddedPgExtension pg) Creates a new extension using a managed, embedded postgres database. Using this method requires thede.softwareforge.testing:pg-embeddedcomponent and the postgres JDBC driver on the class path.It references an embedded pg extension, which must be added to the class independently and can be managed as a per-class extension, not a per-method extension:
@RegisterExtension public static EmbeddedPgExtension pg = MultiDatabaseBuilder.instanceWithDefaults().build(); @RegisterExtension public JdbiExtension postgres = JdbiExtension.postgres(pg);This is the most efficient way to run multiple tests within the same class that use a postgres database.
- Parameters:
pg- Reference to an embedded postgres database. This extension must be separately added to the test class.- Returns:
- A
JdbiExtensionconnected to a managed postgres data source. - See Also:
-
externalPostgres
public static JdbiExtension externalPostgres(@Nonnull String hostname, @Nullable Integer port, @Nonnull String database, @Nullable String username, @Nullable String password) Creates an extension that uses an external (outside the scope of an unit test class) postgres database. Using this method requires the postgres JDBC driver on the classpath.- Parameters:
hostname- Hostname of the database.port- Port of the database. Can be null, in that case the default port is used.database- Database name.username- Username to access the database. Can be null.password- Password to access the database. Can be null.- Returns:
- A
JdbiExtensionconnected to an external postgres data source. - See Also:
-
otjEmbeddedPostgres
Creates a new extension using a managed, embedded postgres database. Using this method requires thecom.opentable.components:otj-pg-embeddedcomponent and the postgres JDBC driver on the class path.@RegisterExtension public JdbiExtension postgres = JdbiExtension.otjEmbeddedPostgres();Compared to the
postgres(EmbeddedPgExtension)method, this extension spins up a new postgres instance for each test method and is slower. It should only be used to migrate existing code that uses the JUnit 4JdbiRulequickly to JUnit 5.- Returns:
- A
JdbiExtensionconnected to a managed postgres data source. - See Also:
-
h2
Creates a new extension using the H2 database. Each call to this method creates a new database which is transient and in-memory. Using this method requires thecom.h2database:h2component on the classpath.@RegisterExtension public JdbiExtension h2 = JdbiExtension.h2();- Returns:
- A
JdbiExtensionconnected to a managed h2 data source. - See Also:
-
sqlite
Creates a new extension using the SQLite database. Each call to this method creates a new database which is transient and in-memory. Using this method requires theorg.xerial:sqlite-jdbccomponent on the classpath.@RegisterExtension public JdbiExtension sqlite = JdbiExtension.sqlite();- Returns:
- A
JdbiExtensionconnected to a managed sqlite data source. - See Also:
-
getJdbi
Returns aJdbiinstance linked to the data source used by this extension. There is only a single Jdbi instance and multiple calls to this method return the same instance.- Returns:
- A
Jdbiinstance.
-
getUrl
Returns a JDBC url representing the data source used by this extension. This url is database-specific and may or may not be used to connect to the data source outside testing code that uses this extension (e.g. theJdbiSqliteExtensionreturns a constant uri for all database instances).- Returns:
- A string representing the JDBC URL.
-
installPlugins
When creating theJdbiinstance, call theJdbi.installPlugins()method, which loads all plugins discovered by theServiceLoaderAPI.- Returns:
- The extension itself for chaining method calls.
-
enableLeakChecker
Enable tracking of cleanable resources and handles when running tests. This is useful to find code paths where JDBI managed resources are not correctly handled and "leak" out.- Parameters:
enable- If true, enables the leak checker, otherwise disables it.
-
withPlugin
Install aJdbiPluginwhen creating theJdbiinstance.- Parameters:
plugin- A plugin to install.- Returns:
- The extension itself for chaining method calls.
-
withPlugins
Install multipleJdbiPlugins when creating theJdbiinstance.- Parameters:
pluginList- One or moreJdbiPlugininstances.- Returns:
- The extension itself for chaining method calls.
-
withInitializer
Sets aJdbiExtensionInitializerto initialize theJdbiinstance or the attached data source before running a test.- Returns:
- The extension itself for chaining method calls.
-
openHandle
Open a newHandleto the used data source. Multiple calls to this method return multiple instances that are all connected to the same data source.- Returns:
- A
Handleobject. The handle must be closed to release the database connection.
-
withConfig
public final <C extends JdbiConfig<C>> JdbiExtension withConfig(Class<C> configClass, Consumer<C> configurer) Set aJdbiConfigparameter when creating theJdbiinstance. AnyJdbiConfigtype can be referenced in this method call.@RegisterExtension public JdbiExtension h2Extension = JdbiExtension.h2().withPlugin(new SqlObjectPlugin()) .withConfig(RowMappers.class, r -> r.register(Foo.class, new FooMapper());- Type Parameters:
C- The config type. Must extendJdbiConfig.- Parameters:
configClass- A class instance which must implementJdbiConfig.configurer- AConsumerto access theJdbiConfiginstance.- Returns:
- The extension itself for chaining method calls.
-
attach
Convenience method to attach an extension (such as a SqlObject) to the shared handle. -
beforeEach
- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
-
beforeAll
- Specified by:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceAfterEachCallback- Throws:
Exception
-
afterAll
- Specified by:
afterAllin interfaceAfterAllCallback- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - Specified by:
supportsParameterin interfaceParameterResolver
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - Specified by:
resolveParameterin interfaceParameterResolver
-