我正在尝试基于一组给定的JUnit测试来测试我的hibernate映射.但是,以下测试失败了.
@Test
public void testEntity3Constraint() {
expectedException.expect(PersistenceException.class);
expectedException.expectCause(isA(ConstraintViolationException.class));
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
// Entity #3
IUplink ent3_1 = daoFactory.getUplinkDAO().findById(testData.entity3_1Id);
ent3_1.setName(TestData.N_ENT3_2);
em.persist(ent3_1);
em.flush();
} finally {
tx.rollback();
}
}
这是我得到的例外:
Expected: (an instance of javax.persistence.PersistenceException and exception with cause is an
instance of org.hibernate.exception.ConstraintViolationException)
but: exception with cause is an instance of org.hibernate.exception.ConstraintViolationException cause
is a org.hibernate.PersistentObjectException
正如您所看到的,Constraint Violations和Persistence Exceptions在实例和原因方面进行了交换.当我调用entitymanager.persist时抛出异常.我怎样才能获得预期的例外?
我不能改变预期的异常,也不想改变.我需要找到一种方法来获得测试所期望的异常.
提前致谢!