2017-03-25 4 views
4

주어진 JUnit 테스트 집합을 기반으로 최대 절전 모드 매핑을 테스트하려고합니다. 그러나 다음 테스트는 실패합니다. Hibernate JPA 지속성 예외

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 

당신은 제한 조건 위반 및 지속성 예외를 볼 수 있듯이

이 인스턴스와 원인의 측면에서 교환됩니다
@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(); 
    } 
} 

내가지고있어 예외입니다. entitymanager.persist에 전화 할 때 예외가 발생합니다. 예상되는 예외는 어떻게 얻을 수 있습니까?

예상되는 예외를 변경할 수 없으며 원하는대로 변경할 수 없습니다. 테스트에서 예상되는 예외를 얻을 수있는 방법을 찾아야합니다.

미리 감사드립니다.

+0

왜'expectedException.expect (ConstraintViolationException.class);'할 수 없습니까? – developer

+0

테스트가 제공되며이를 변경할 수 없으므로 테스트에 필요한 예외를 얻을 수있는 방법을 찾고 있습니다. – Syn

답변

0

당신은 예상하고 있습니다 javax.persistence.PersistenceException
그러나 비슷하지만 다른 org.hibernate.PersistentObjectException을 얻을 수 있습니다.

테스트에서 기대하는 예외 만 변경하십시오.

+0

감사합니다. 직접해볼 수있었습니다. 나는 그 반대가 필요하다. 예상 한 것과 일치시키기 위해 Throw Exception을 변경하고 싶습니다. – Syn

+0

다음에는 고유 한 EntityManager를 만들어야합니다. –