Hibernate 2nd level data cache and integration/acceptance testing Hibernate 2nd level data cache and integration/acceptance testing selenium selenium

Hibernate 2nd level data cache and integration/acceptance testing


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.


I had a requirement of implementing read only caching for few beans like country, region etc..

To check if they are really getting cached I have written a integration test with using spring. The test is not so proper, it is just a verification of what I wanted to get. You can use this as a hint and implement your own.

You can read here for an article on how to write an integration test using spring.

@Configurable(autowire = Autowire.BY_NAME)@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext.xml" })public class HibernateCachingTestIntg {    @Autowired    private ConfigurationDAO configurationDAO;    @Autowired    private CountryDAO countryDAO;    @Test    public void testGetCountries() {        for (int i = 0; i < 5; i++) {            StopWatch sw = new StopWatch("C");            sw.start();            countryDAO.listCountries();            sw.stop();            System.out.println(sw);        }    }    @Test    public void testGetRegionList() {        for (int i = 0; i < 5; i++) {            StopWatch sw = new StopWatch("R");            sw.start();            configurationDAO.getRegionList();            sw.stop();            System.out.println(sw);        }    }}

And here is the output:-

StopWatch 'C': running time (millis) = 217; [] took 217 = 100%StopWatch 'C': running time (millis) = 15; [] took 15 = 100%StopWatch 'C': running time (millis) = 16; [] took 16 = 100%StopWatch 'C': running time (millis) = 15; [] took 15 = 100%StopWatch 'C': running time (millis) = 16; [] took 16 = 100%StopWatch 'R': running time (millis) = 201; [] took 201 = 100%StopWatch 'R': running time (millis) = 15; [] took 15 = 100%StopWatch 'R': running time (millis) = 0; [] took 0 = 0%StopWatch 'R': running time (millis) = 16; [] took 16 = 100%StopWatch 'R': running time (millis) = 15; [] took 15 = 100%

As you can see here, the query takes more time to execute for the first time and less time afterwords. If you turn the query logger on, you can see that the SQL is fired only for the first time.


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.