Class EmbeddedDatabaseExtension
java.lang.Object
com.github.mjeanroy.dbunit.integration.spring.jupiter.EmbeddedDatabaseExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback
,org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeAllCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
public class EmbeddedDatabaseExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
A JUnit Jupiter extension that can be used to start/stop an embedded database.
Note that this extension can be used with:
Here is an example using the
Note that this extension can be used with:
-
The
ExtendWith
annotation, in this case the embedded server will be started before all tests and stopped after all tests. -
Using the
RegisterExtension
annotation, in this case the embedded server will be-
Started before all tests and stopped after all tests if the extension
is declared as
static
or the test class is usedTestInstance.Lifecycle.PER_CLASS
mode. -
Started before each test and stopped after each test if the extension
is not declared as
static
and the test class is used withTestInstance.Lifecycle.PER_METHOD
mode (the default).
-
Started before all tests and stopped after all tests if the extension
is declared as
EmbeddedDatabase
into test methods.
Here is an example using the
RegisterExtension
annotation:
class MyDaoTest {
@RegisterExtension
static EmbeddedDatabaseExtension extension = new EmbeddedDatabaseExtension(
new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.addScript("classpath:/sql/init.sql")
.addScript("classpath:/sql/data.sql")
.build()
);
@Test
void test1(EmbeddedDatabase db) throws Exception {
Assertions.assertEquals(count(db.getConnection()), 2);
}
}
Note that this extension can also be used with the EmbeddedDatabaseConfiguration
annotation. Here is the exact
same example as below:
@ExtendWith(EmbeddedDatabaseExtension.class)
@EmbeddedDatabaseConfiguration(
generateUniqueName = true,
scripts = {"classpath:/sql/init.sql", "classpath:/sql/data.sql"}
)
class MyDaoTest {
@Test
void test1(EmbeddedDatabase db) throws Exception {
Assertions.assertEquals(count(db.getConnection()), 2);
}
}
- See Also:
-
EmbeddedDatabaseConfiguration
EmbeddedDatabase
EmbeddedDatabaseBuilder
-
Constructor Summary
ConstructorsConstructorDescriptionCreate the extension, a defaultEmbeddedDatabase
will be used.EmbeddedDatabaseExtension
(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create the extension, the givenEmbeddedDatabase
will be used. -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
Constructor Details
-
EmbeddedDatabaseExtension
public EmbeddedDatabaseExtension()Create the extension, a defaultEmbeddedDatabase
will be used. -
EmbeddedDatabaseExtension
public EmbeddedDatabaseExtension(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create the extension, the givenEmbeddedDatabase
will be used.- Parameters:
db
- The given embedded database.
-
-
Method Details
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
-
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
-