工作CASE:我加载用户对象@PostConstruct,并试图让角色任何测试方法的时候,我得到惰性初始模式例外,但是当装载任何测试方法,然后让角色的用户对象,一切工作正常。懒加载不@PostConstruct
需要量:我希望能够让懒惰初始化在测试方法正常工作,而不需要在每个测试方法加载的对象,也没有在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);
}
}
它工作正常,但init方法需要公开,感谢您的快速回复。 –
更正,C&P错误,谢谢。 –