views:

241

answers:

1

I was wondering if anyone tried using JMockit Hibernate Emulation?

Jmockit documentation says that when Hibernate Emulation tests are run, they won't use the O/R mapping information. So, this means it doesn't test O/R mappings, HQL query strings, Native queries, etc. Then what really are the benefits of Hibernate Emulation? One can just create MyDAO mock and use that for testing. Why bother with hibenrate emulation just mock out all the DAOs. What do you think ?

Thanks.

A: 

Check out orderMngr.domain.customer.CustomerTest in the "jmockit/samples/orderMngmntWebapp" sample test suite, found in the JMockit full distribution. This test class relies on Hibernate Emulation. It can be run through the "sampleTests" target in "jmockit/build.xml".

My motivation for creating this tool was mainly that SessionFactory creation takes too long when the project has hundreds of mapped entities. (It took about 20-30 seconds in a project with 400+ entity classes.) This in a suite of integration tests, of course. For a suite of unit tests this tool is not useful.

The idea was that a developer could quickly run the integration tests in a local development environment to test business logic but not persistence, while letting the automated build server run the full test suite regularly, without emulation.

Note that HQL queries are still tested when using Hibernate emulation. The fake implementation will parse HQL strings and execute the query against entity instances "persisted" in memory. O/R mapping information is ignored, though.

Mocking DAOs is perfectly valid as well, but then your tests will never actually test the O/R mapping, HQL queries, or actual database access. Using Hibernate Emulation you don't create mocks, but can run the tests over a fake Hibernate implementation that provides something similar to an in-memory database.

Rogerio