Package io.dropwizard.testing.junit
Class DAOTestRule
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- io.dropwizard.testing.junit.DAOTestRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
@Deprecated public class DAOTestRule extends org.junit.rules.ExternalResource
Deprecated.Deprecated since Dropwizard 2.0.0. Please migrate to JUnit 5 andDAOTestExtension.A JUnit rule for testing DAOs and Hibernate entities. It allows to quickly test the database access code without starting the Dropwizard infrastructure.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"); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDAOTestRule.BuilderDeprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidafter()Deprecated.protected voidbefore()Deprecated.org.hibernate.SessionFactorygetSessionFactory()Deprecated.Returns the current active session factory for injecting to DAOs.voidinTransaction(Runnable action)Deprecated.Performs an action in a transaction<T> TinTransaction(Callable<T> call)Deprecated.Performs a call in a transactionstatic DAOTestRule.BuildernewBuilder()Deprecated.Creates a new builder forDAOTestRule, which allows to customize aSessionFactoryby different parameters.
-
-
-
Method Detail
-
newBuilder
public static DAOTestRule.Builder newBuilder()
Deprecated.Creates a new builder forDAOTestRule, which allows to customize aSessionFactoryby different parameters. By default uses the H2 database in the memory mode.- Returns:
- a new
DAOTestRule.Builder
-
before
protected void before() throws ThrowableDeprecated.- Overrides:
beforein classorg.junit.rules.ExternalResource- Throws:
Throwable
-
after
protected void after()
Deprecated.- Overrides:
afterin classorg.junit.rules.ExternalResource
-
getSessionFactory
public org.hibernate.SessionFactory getSessionFactory()
Deprecated.Returns the current active session factory for injecting to DAOs.- Returns:
SessionFactorywith an open session.
-
inTransaction
public <T> T inTransaction(Callable<T> call)
Deprecated.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)
Deprecated.Performs an action in a transaction- Parameters:
action- the action
-
-