public class DAOTestRule
extends org.junit.rules.ExternalResource
Example:
@Rule
public DAOTestRule daoTestRule = DAOTestRule.newBuilder()
.addEntityClass(Person.class)
.build();
private PersonDAO personDAO;
@Before
public void setUp() throws Exception {
personDAO = new PersonDAO(daoTestRule.getSessionFactory());
}
@Test
public void createPerson() {
Person wizard = daoTestRule.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 |
DAOTestRule.Builder |
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
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 DAOTestRule.Builder |
newBuilder()
Creates a new builder for
DAOTestRule , which allows to customize a SessionFactory
by different parameters. |
public static DAOTestRule.Builder newBuilder()
DAOTestRule
, which allows to customize a SessionFactory
by different parameters. By default uses the H2 database in the memory mode.DAOTestRule.Builder
protected void before() throws Throwable
before
in class org.junit.rules.ExternalResource
Throwable
protected void after()
after
in class org.junit.rules.ExternalResource
public 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 © 2019. All rights reserved.