JP Hibernate containment exception

I am trying to verify hibernate mapping based on a set of given JUnit tests. However, the following test fails.

@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();
    }
}

This is the exception I get:

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

As you can see, Exceptions to limitations and constancy restrictions are replaced in terms of instance and reason. An exception is thrown on a call entitymanager.persist. How can I get the expected exception?

I can’t change the expected exception and don’t want to. I need to find a way to get the exception expected as a result of the test.

Thanks in advance!

+4
source share
1 answer

It is expected javax.persistence.PersistenceException
but get similar, but different org.hibernate.PersistentObjectException.

, .

0

Source: https://habr.com/ru/post/1673206/


All Articles