Package io.dropwizard.testing.junit5
Class DAOTestExtension
- java.lang.Object
-
- io.dropwizard.testing.junit5.DAOTestExtension
-
- All Implemented Interfaces:
DropwizardExtension
public class DAOTestExtension extends Object implements DropwizardExtension
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 Modifier and Type Class Description static class
DAOTestExtension.Builder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
after()
Executed after test method or class.void
before()
Executed before test method or class.org.hibernate.SessionFactory
getSessionFactory()
Returns the current active session factory for injecting to DAOs.void
inTransaction(Runnable action)
Performs an action in a transaction<T> T
inTransaction(Callable<T> call)
Performs a call in a transactionstatic DAOTestExtension.Builder
newBuilder()
Creates a new builder forDAOTestRule
, which allows to customize aSessionFactory
by different parameters.
-
-
-
Method Detail
-
newBuilder
public static DAOTestExtension.Builder newBuilder()
Creates a new builder forDAOTestRule
, which allows to customize aSessionFactory
by different parameters. By default uses the H2 database in the memory mode.- Returns:
- a new
DAOTestRule.Builder
-
before
public void before() throws Throwable
Description copied from interface:DropwizardExtension
Executed before test method or class.- Specified by:
before
in interfaceDropwizardExtension
- Throws:
Throwable
-
after
public void after()
Description copied from interface:DropwizardExtension
Executed after test method or class.- Specified by:
after
in interfaceDropwizardExtension
-
getSessionFactory
public org.hibernate.SessionFactory getSessionFactory()
Returns the current active session factory for injecting to DAOs.- Returns:
SessionFactory
with an open session.
-
inTransaction
public <T> T inTransaction(Callable<T> call)
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
public void inTransaction(Runnable action)
Performs an action in a transaction- Parameters:
action
- the action
-
-