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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With