How to perform TDD using Hibernate

Asked

Viewed 370 times

4

I would like to know how to perform TDD using Hibernate. I have been informed that this ORM stores memory before recording into possible, so I wanted to know how to test a data that is in memory. For example: I run the save command and do a test to check if this data is saved, it is possible?

3 answers

4


You have to understand the following, the cache is tied to the Persistencecontext that is created when you create an Entitymanager.

You can easily use Junit with Entitymanager to test your DAO. What you should do is:

  1. Add HSQLDB as dependency or some other database in memory
  2. Having persistence.xml only for the test, in which case, simply add it inside a test directory, in the case of Maven, that automatically this context change would be made.
  3. Create a static Entitymanagerfactory:

    private Static Entitymanagerfactory entityManagerFactory;

  4. Create a annotated static method with @Beforeclass that will create Entitymanagerfactory:

    @Beforeclass public Static void createPersistenceUnit() { entityManagerFactory = Persistence.createEntityManagerFactory("PU") }

  5. Create a annotated static method with @Afterclass to kill Entitymanagerfactory:

    @Afterclass public Static void closePersistenceUnit() { entityManagerFactory.close(); }

  6. Create an annotated method with @Before to start Entitymanager each test:

    @Before public void beforeTest() { entityManager = getEntityManagerFactory(). createEntityManager(); }

  7. Create a method with @After to finish Entitymanager after each test:

    @After public void finishTest() { entityManager.close(); }

This way you will have an Entitymanager for each test and your first level cache will be no problem.

This post is a little old, but talks about it: http://uaihebert.com/tdd-com-hsqldb-jpa-e-hibernate/

4

It is not possible to use only the first level cache of Hibernate for use in test cases. The best approach to this problem is to configure Hibernate so that it uses some database in memory.

Some in-memory databases you can use are: HSQLDB, Apache Derby or H2DB.

Just create a DataSource for any of these Bds and use it in the Hibernate configuration used in your test cases.

Example: http://www.java2s.com/Tutorial/Java/0350__Hibernate/HibernateandHSQL.htm

  • This is the appropriate strategy for most situations.

1

I think you’re going down the road of testing the technology and not the business domain of your problem.

I think you could try to "mock" the Hibernate with a mockite for example and not worry so much with the technology and yes with the rules of business, after all this is what we use the TDD... :-)

  • Yeah, but I wanted to save to object and test a validation of the type, if that object already exists in the database. More to test the method.

  • 1

    So you keep testing the technology, unless the business rule is related to having the object in memory or not... It may be that "mock" the method that returns the object from memory, help you test the other business methods, take a look at the mockite, can help some examples of it...

  • The rule would be like, if the object already exists when consulted to the BD, it does not insert, things like this.

  • It seems to me something like an update. But if this is not the case then it could be something for a cache outside of Hibernate... maybe...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.