Java异常持久化_java – Hibernate JPA持久性异常

我正在尝试基于一组给定的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时抛出异常.我怎样才能获得预期的例外?

我不能改变预期的异常,也不想改变.我需要找到一种方法来获得测试所期望的异常.

提前致谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值