Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 2nd level data cache and integration/acceptance testing

I have an JPA/Hibernate/Spring/Tomcat web application with second level data cache enabled for performance reasons. And the cache does its work very well!

I also have a Cucumber test suite which adds some test data directly to the application's database and then performs some Selenium steps. Of course it fails as the application doesn't see the updates because of the 2nd level cache.

I know I can make special build for testing with cache disabled (by passing some boolean property for Maven filtering or similar) But there are a lot of @Cache annotated entities so disabling the cache makes the application fail with exception "Second level cache is not enabled".

Another approach could be to use ehcache remoting to clear the cache or configure it with zero object lifetime or similar.

I can also create my test data using application UI only but this adds unnecessary complexity to the test cases so I prefer to write them to the DB before test run.

Could anybody share their approach to integration-test applications with 2nd level data cache enabled?

like image 963
ike3 Avatar asked Nov 14 '22 06:11

ike3


1 Answers

If you need to test the second level cache with unit test you have to be sure to close the session and open it each time you are calling dao method. Otherwise you will use first level cache which exist only in the scope of one/current hibernate session.

like image 94
Tiko Avatar answered Nov 16 '22 04:11

Tiko