Testing Hibernate DAO, without building the universe around it.

Posted by Varun Mehta on Stack Overflow See other posts from Stack Overflow or by Varun Mehta
Published on 2010-05-27T18:00:06Z Indexed on 2010/05/27 18:21 UTC
Read the original article Hit count: 144

Filed under:
|
|
|
|

We have an application built using spring/Hibernate/MySQL, now we want to test the DAO layer, but here are a few shortcomings we face.

Consider the use case of multiple objects connected to one another, eg: Book has Pages.

  • The Page object cannot exist without the Book as book_id is mandatory FK in Page.
  • For testing a Page I have to create a Book.

This simple usecase is easy to manage, but if you start building a Library, till you don't create the whole universe surrounding the Book and Page, you cannot test it!

So to test Page;

  • Create Library
  • Create Section
  • Create Genre
  • Create Author
  • Create Book
  • Create Page
  • Now test Page.

Is there an easy way to by pass this "universe creation" and just test he page object in isolation. I also want to be able to test HQLs related to Page. eg:

SELECT new com.test.BookPage (book.id, page.name) FROM Book book, Page page.

JUnit is supposed to run in isolation, so I have to write the whole test case to create the Page. Any tips will be useful.

© Stack Overflow or respective owner

Related posts about java

Related posts about unit-testing