私は与えられたJUnitテストのセットに基づいて私のハイバネートマッピングをテストしようとしています。しかし、次のテストは失敗します。Hibernate JPA永続例外
@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
<org.hibernate.PersistentObjectException: detached entity passed to persist: dst.ass1.jpa.model.impl.UplinkImpl>
is a org.hibernate.PersistentObjectException
を使用すると、制約違反と持続性の例外は、インスタンスと原因の面で交換され見ることができるように。 entitymanager.persist
を呼び出すと例外がスローされます。予想される例外はどのようにして得られますか?
私は予想される例外を変更することはできませんし、したくありません。私はテストによって予期される例外を得る方法を見つける必要があります。
ありがとうございます!
なぜ 'expectedException.expect(ConstraintViolationException.class);'を実行できないのですか?何か不足していますか? – developer
テストが行われていますが、私はそれらを変更することはできません。だから、テストが必要とする例外を得る方法を探しています。 – Syn