CASE: i load the user object in @PostConstruct, and when trying to get the roles in any test method, i get lazyinitialization exception, but when loading the user object in any test method and then getting the roles, everything works fine.
REQUIREMENT: i want to be able to make lazy initialization works fine in test methods without the need of loading the object in each test method, and also without the workaround of loading the collection in the init method, are there any good solution for such issue in unit test ?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/applicationContext.xml",
"classpath:/META-INF/spring/applicationSecurity.xml" })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class DepartmentTest extends
AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private EmployeeService employeeService;
private Employee testAdmin;
private long testAdminId;
@PostConstruct
private void init() throws Exception {
testAdminId = 1;
testAdmin = employeeService.getEmployeeById(testAdminId);
}
@Test
public void testLazyInitialization() throws Exception {
testAdmin = employeeService.getEmployeeById(testAdminId);
//if i commented the above assignment, i will get lazyinitialiaztion exception on the following line.
Assert.assertTrue(testAdmin.getRoles().size() > 0);
}
}