工作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錯誤,謝謝。 –