public class DAOTestExtension extends Object implements DropwizardExtension
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");
}
| Modifier and Type | Class and Description |
|---|---|
static class |
DAOTestExtension.Builder |
| Modifier and Type | Method and 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.
|
<T> T |
inTransaction(Callable<T> call)
Performs a call in a transaction
|
void |
inTransaction(Runnable action)
Performs an action in a transaction
|
static DAOTestExtension.Builder |
newBuilder()
Creates a new builder for
DAOTestExtension, which allows to customize a SessionFactory
by different parameters. |
public static DAOTestExtension.Builder newBuilder()
DAOTestExtension, which allows to customize a SessionFactory
by different parameters. By default uses the H2 database in the memory mode.DAOTestExtension.Builderpublic void before()
throws Throwable
DropwizardExtensionbefore in interface DropwizardExtensionThrowablepublic void after()
DropwizardExtensionafter in interface DropwizardExtensionpublic org.hibernate.SessionFactory getSessionFactory()
SessionFactory with an open session.public <T> T inTransaction(Callable<T> call)
T - the type of the returned resultcall - the callpublic void inTransaction(Runnable action)
action - the actionCopyright © 2021. All rights reserved.