Class JdbiExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
- Direct Known Subclasses:
JdbiExternalPostgresExtension
,JdbiH2Extension
,JdbiOtjPostgresExtension
,JdbiPostgresExtension
,JdbiSqliteExtension
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 TypeMethodDescriptionvoid
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) final <T> T
Convenience method to attach an extension (such as a SqlObject) to the shared handle.void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) static JdbiExtension
Creates an extension that uses an external (outside the scope of an unit test class) postgres database.final Jdbi
getJdbi()
Returns aJdbi
instance linked to the data source used by this extension.final Handle
Returns a sharedHandle
used by the extension.abstract String
getUrl()
Returns a JDBC url representing the data source used by this extension.static JdbiExtension
h2()
Creates a new extension using the H2 database.final JdbiExtension
When creating theJdbi
instance, call theJdbi.installPlugins()
method, which loads all plugins discovered by theServiceLoader
API.final Handle
Open a newHandle
to the used data source.static JdbiOtjPostgresExtension
Creates a new extension using a managed, embedded postgres database.static JdbiExtension
postgres
(de.softwareforge.testing.postgres.junit5.EmbeddedPgExtension pg) Creates a new extension using a managed, embedded postgres database.resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) static JdbiExtension
sqlite()
Creates a new extension using the SQLite database.boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) final <C extends JdbiConfig<C>>
JdbiExtensionwithConfig
(Class<C> configClass, Consumer<C> configurer) Set aJdbiConfig
parameter when creating theJdbi
instance.final JdbiExtension
withInitializer
(JdbiExtensionInitializer initializer) Sets aJdbiExtensionInitializer
to initialize theJdbi
instance or the attached data source before running a test.final JdbiExtension
withPlugin
(JdbiPlugin plugin) Install aJdbiPlugin
when creating theJdbi
instance.final JdbiExtension
withPlugins
(JdbiPlugin... pluginList) Install multipleJdbiPlugin
s when creating theJdbi
instance.
-
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-embedded
component 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
JdbiExtension
connected 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
JdbiExtension
connected 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-embedded
component 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 4JdbiRule
quickly to JUnit 5.- Returns:
- A
JdbiExtension
connected 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:h2
component on the classpath.@RegisterExtension public JdbiExtension h2 = JdbiExtension.h2();
- Returns:
- A
JdbiExtension
connected 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-jdbc
component on the classpath.@RegisterExtension public JdbiExtension sqlite = JdbiExtension.sqlite();
- Returns:
- A
JdbiExtension
connected to a managed sqlite data source. - See Also:
-
getJdbi
Returns aJdbi
instance 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
Jdbi
instance.
-
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. theJdbiSqliteExtension
returns a constant uri for all database instances).- Returns:
- A string representing the JDBC URL.
-
installPlugins
When creating theJdbi
instance, call theJdbi.installPlugins()
method, which loads all plugins discovered by theServiceLoader
API.- Returns:
- The extension itself for chaining method calls.
-
withPlugin
Install aJdbiPlugin
when creating theJdbi
instance.- Parameters:
plugin
- A plugin to install.- Returns:
- The extension itself for chaining method calls.
-
withPlugins
Install multipleJdbiPlugin
s when creating theJdbi
instance.- Parameters:
pluginList
- One or moreJdbiPlugin
instances.- Returns:
- The extension itself for chaining method calls.
-
withInitializer
Sets aJdbiExtensionInitializer
to initialize theJdbi
instance or the attached data source before running a test.- Returns:
- The extension itself for chaining method calls.
-
openHandle
Open a newHandle
to the used data source. Multiple calls to this method return multiple instances that are all connected to the same data source.- Returns:
- A
Handle
object. 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 aJdbiConfig
parameter when creating theJdbi
instance. AnyJdbiConfig
type 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
- AConsumer
to access theJdbiConfig
instance.- 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:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
Exception
-
afterEach
- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
supportsParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
-
resolveParameter
public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
-