ケース: @PostConstructでユーザーオブジェクトをロードし、任意のテストメソッドでロールを取得しようとすると、lazyinitialization例外が発生しますが、任意のテストメソッドでユーザーオブジェクトをロードしてからロールを取得すると、すべてが正常に機能します。
要件: 各テストメソッドにオブジェクトをロードする必要がなく、またinitメソッドにコレクションをロードする回避策なしで、テストメソッドでレイジー初期化を正常に機能させることができるようにしたいのですが、そのような問題の良い解決策はありますか?ユニットテストで?
@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);
}
}