嗨,我有一个单元测试应该测试一个 cronjob
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:WebContent/WEB-INF/applicationContext.xml" })
@TransactionConfiguration(transactionManager = "hibernateTransactionManager")
@Transactional
public class cronTest {}
但由于事务的原因,cronjob 看不到测试的数据。但是当我删除@Transactional Hibernate 时说:
org.hibernate.HibernateException: No Session found for current thread
当我自己承诺时
sessionFactory.getCurrentSession().getTransaction().commit();
sessionFactory.getCurrentSession().beginTransaction();
测试最终失败
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
如何在没有事务的情况下进行单元测试?
测试做了这样的事情:
userDao.clear();
loadeduser = userService.getUserById(createUser.getUserID());
assertTrue(loadeduser.getAuthoritiesEntities().contains(adminRole));
assertTrue(loadeduser.getAuthoritiesEntities().contains(userRole));
// Wait for auto unsign
TestUtils.sleep(10000);
userDao.clear();
loadeduser = userService.getUserById(createUser.getUserID());
assertFalse(loadeduser.getAuthoritiesEntities().contains(adminRole));
assertTrue(loadeduser.getAuthoritiesEntities().contains(userRole));
它检查过期角色的自动取消签名是否有效,石英作业每 5 秒运行一次