• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate without a database

 
serge masse
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dudes and dudettes,
We have an application using Hibernate with Oracle but now we want to run some tests without the database being present and having the queries returning hardcoded values. What is the easiest way to do this with Hibernate?

thanks,
may your horse always shit downwind,
serge
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet is to use mock objects like jMock and EasyMock. Both are fairly equivalent, though I prefered jMock's API when evaluating them six months ago. Where you mock depends on what you're testing.

Assuming you are using the DAO pattern, you can test your business logic by mocking your DAOs and you can test your DAOs by mocking Hibernate. I haven't done the latter, but it should work exactly the same.

Basically, mock objects allow you to replace the real Hibernate Session implementation with a mock one. Mock objects are unlike stubs that simply do nothing. Instead, for each test you tell the mock what method calls it should expect and what to do in response (return something, throw an exception, etc). Then you test the object that's hooked up to the mock, and the mock will record the method calls it receives. At the end of the test, the mock compares the expected calls to the ones received and throws an exception if they don't match.

For example, to test that UserDao.get(Integer id) throws UserNotFoundException when no User matches the ID, you'd tell the mock Session to expect a load(Object id) call and return null in response. Next, you call UserDao.get(id) in a try-catch block. If you catch UserNotFoundException, the test passes. Otherwise, it fails. If the mock receives any other method call, the test will also fail.

The combination of mock objects and test fixtures for creating objects to return from mock calls is very powerful and flexible.
 
serge masse
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David,
It looks like this is what I was looking for.

Javaranch lives up to its reputation once again.
 
Jean-Fran´┐Żois Sublet
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm interested on how to mock hibernate using mock objects. Do you achieve this ?

Thx,
Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic