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, org.junit.jupiter.api.extension.TestInstantiationAwareExtension
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
ExtendWithannotation, in this case the embedded server will be started before all tests and stopped after all tests. -
Using the
RegisterExtensionannotation, in this case the embedded server will be-
Started before all tests and stopped after all tests if the extension
is declared as
staticor the test class is usedTestInstance.Lifecycle.PER_CLASSmode. -
Started before each test and stopped after each test if the extension
is not declared as
staticand the test class is used withTestInstance.Lifecycle.PER_METHODmode (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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope -
Constructor Summary
ConstructorsConstructorDescriptionCreate the extension, a defaultEmbeddedDatabasewill be used.EmbeddedDatabaseExtension(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create the extension, the givenEmbeddedDatabasewill be used. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext context) voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) booleansupportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
EmbeddedDatabaseExtension
public EmbeddedDatabaseExtension()Create the extension, a defaultEmbeddedDatabasewill be used. -
EmbeddedDatabaseExtension
public EmbeddedDatabaseExtension(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create the extension, the givenEmbeddedDatabasewill be used.- Parameters:
db- The given embedded database.
-
-
Method Details
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterEachin 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:
supportsParameterin 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:
resolveParameterin interfaceorg.junit.jupiter.api.extension.ParameterResolver
-