• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate without a database

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thx,
Jeff
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic