2017-03-25 22 views
4

私は与えられた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を呼び出すと例外がスローされます。予想される例外はどのようにして得られますか?

私は予想される例外を変更することはできませんし、したくありません。私はテストによって予期される例外を得る方法を見つける必要があります。

ありがとうございます!

+0

なぜ 'expectedException.expect(ConstraintViolationException.class);'を実行できないのですか?何か不足していますか? – developer

+0

テストが行​​われていますが、私はそれらを変更することはできません。だから、テストが必要とする例外を得る方法を探しています。 – Syn

答えて

0

あなたはjavax.persistence.PersistenceException
を期待したが同様のまだ別のorg.hibernate.PersistentObjectExceptionを取得しています。

テストで期待している例外を変更してください。

+0

ありがとう、私はそれを自分でやることができました。私はそれの反対が必要です。予期されるものと一致させるためにスローされたExceptionを変更したい – Syn

+0

あなた自身のEntityManagerを作成する必要があります。 –

関連する問題