Dropwizard + Hibernate (Test DAO)

3,585 views
Skip to first unread message

CAB

unread,
Nov 13, 2012, 3:53:07 PM11/13/12
to dropwiz...@googlegroups.com
I have a prototype service using (dropwizard + hibernate), and I have followed this tutorial regarding dropwizard testing.

I want to test the DAO layer. What is the recommended approach? Do I have to spin up an instance of my service pointed against a test database?

Thanks,
-Carlo

Tatu Saloranta

unread,
Nov 13, 2012, 4:55:52 PM11/13/12
to dropwiz...@googlegroups.com
What I have done is to spin up service instance programmatically
(rather easy to do) from within test; and use an in-memory SQL
database (like h2) instead of "real" database. This requires keeping
design sort of lingua franca SQL, which may not always be feasible;
but when it is, things work pretty nicely.

-+ Tatu +-

Diego Magalhães

unread,
Nov 13, 2012, 4:58:23 PM11/13/12
to dropwiz...@googlegroups.com
Mock it... use Mockito and DBUnit, a good approach is shown in this example http://www.reverttoconsole.com/blog/spring/mock-unit-testing-services-and-daos-with-mockito-and-spring-3/

Diego Magalhães
http://diegomagalhaes.com
claro @ +55 21 9411 2823

Tom McKenzie

unread,
Nov 14, 2012, 2:11:10 PM11/14/12
to dropwiz...@googlegroups.com
We have tried this approach but are having problems with transactions.  We get 

org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction

We are using the code from https://github.com/codahale/dropwizard/tree/master/dropwizard-example

Our test looks like this.  We hoped the Transactional annotation would work.

@Test

  @Transactional

  public void testCreate()

  {

  this.person = new Person();

  this.person.setFullName("John Smith");

  this.person.setJobTitle("Operations");

 

  Person newPerson = null;

  try {

  newPerson = this.personDAO.create(this.person);

  } catch (Exception e) {

  e.printStackTrace();

  }

 

  assertNotNull(newPerson);

Coda Hale

unread,
Nov 14, 2012, 2:21:19 PM11/14/12
to dropwiz...@googlegroups.com
That's not at all how @Transactional works. It's *only* for Jersey resource methods, and only if your Jersey configuration includes TransactionalResourceMethodDispatchAdapter as a provider. Please look through the dropwizard-hibernate source code. It's literally less than 300 lines of code.

You'll need to manage your own Hibernate sessions and transactions in your tests.

---
Coda Hale
http://codahale.com


CAB

unread,
Nov 15, 2012, 12:19:53 PM11/15/12
to dropwiz...@googlegroups.com
Yeah. That's what I ended up doing.

public abstract class DropwizardDAOTest {

protected abstract SessionFactory getFactory() throws Exception;
private Session session;
    @Before
    public void setUp() throws Exception
    {
session = this.getFactory().openSession();
ManagedSessionContext.bind(session);
    }
    
@After
public void tearDown() throws Exception
{
session.close();
ManagedSessionContext.unbind(getFactory());
}
}

I also used this link as a concept for creating a Rule to spin the service up & down for the test class. FYI the code in the link is not compatible with latest dropwizard

CAB

unread,
Nov 15, 2012, 4:39:26 PM11/15/12
to dropwiz...@googlegroups.com
Here is a gist with what I did: https://gist.github.com/4081457
Reply all
Reply to author
Forward
0 new messages