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?
Since you’re talking about functional tests via Selenium, you should consider your app as a black box and test it as Selenium was actually a User. So you need to pass data via web interface and then test how the app processes it and shows it afterwards.
Alternative to such application-wide functional tests will be a Behavior Driven Development with tests for different components. Component here is some flow starting from your controller ending with DAO (usually DAO is mocked in such tests which make them pass very fast, but doesn’t test working with database). In this case you have a small set of full environment tests and a large set of BDD tests.