Package io.dropwizard.testing.junit5
Class DAOTestExtension
java.lang.Object
io.dropwizard.testing.junit5.DAOTestExtension
- All Implemented Interfaces:
DropwizardExtension,org.junit.jupiter.api.extension.AfterAllCallback,org.junit.jupiter.api.extension.BeforeAllCallback,org.junit.jupiter.api.extension.Extension
public class DAOTestExtension
extends Object
implements DropwizardExtension, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback
An extension for testing DAOs and Hibernate entities. It allows to quickly
test the database access code without starting the Dropwizard infrastructure.
Example:
public DAOTestExtension daoTestExtension = DAOTestExtension.newBuilder()
.addEntityClass(Person.class)
.build();
private PersonDAO personDAO;
@BeforeEach
public void setUp() throws Exception {
personDAO = new PersonDAO(daoTestRule.getSessionFactory());
}
@Test
public void createPerson() {
Person wizard = daoTestExtension.inTransaction(() -> personDAO.create(new Person("Merlin", "The chief wizard")));
assertThat(wizard.getId()).isGreaterThan(0);
assertThat(wizard.getFullName()).isEqualTo("Merlin");
assertThat(wizard.getJobTitle()).isEqualTo("The chief wizard");
}
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidafter()Executed after test method or class.voidafterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) voidbefore()Executed before test method or class.voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) org.hibernate.SessionFactoryReturns the current active session factory for injecting to DAOs.voidinTransaction(Runnable action) Performs an action in a transaction<T> TinTransaction(Callable<T> call) Performs a call in a transactionstatic DAOTestExtension.BuilderCreates a new builder forDAOTestExtension, which allows to customize aSessionFactoryby different parameters.
-
Method Details
-
newBuilder
Creates a new builder forDAOTestExtension, which allows to customize aSessionFactoryby different parameters. Uses the H2 database in memory mode by default.- Returns:
- a new
DAOTestExtension.Builder
-
before
Description copied from interface:DropwizardExtensionExecuted before test method or class.- Specified by:
beforein interfaceDropwizardExtension- Throws:
Throwable
-
after
public void after()Description copied from interface:DropwizardExtensionExecuted after test method or class.- Specified by:
afterin interfaceDropwizardExtension
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception - Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
getSessionFactory
public org.hibernate.SessionFactory getSessionFactory()Returns the current active session factory for injecting to DAOs.- Returns:
SessionFactorywith an open session.
-
inTransaction
Performs a call in a transaction- Type Parameters:
T- the type of the returned result- Parameters:
call- the call- Returns:
- the result of the call
-
inTransaction
Performs an action in a transaction- Parameters:
action- the action
-