I have some hibernate queries where I expect some objects to be eargerly loaded while others are lazy-loaded.
My current approch for checking for eager loading is measuring the time it takes to access a property.
//Call the method i want to test.
Customer customer = loadCustomerWithProducts();
long start = System.currentTimeMillis();
//Call a method on an object that sould have been eagerly loaded.
customer.getProducts().get(0).getName();
long end = System.currentTimeMillis();
long diff = end - start;
Assert.assertTrue(diff < 50); //Assuming that hitting the database takes longer than 50ms.
This aproach is very flawed, as tests will fail depending on system performance. Is there a better way?
I would use