Java – hibernate JPA persistence exception

I'm trying to test my hibernate mapping based on a given set of JUnit tests However, the following tests failed

@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, constraint violations and persistence exceptions are exchanged in terms of instance and reason When I call entitymanager Exception thrown while persisting How can I get the expected exceptions?

I can't change the expected anomaly, and I don't want to change it I need to find a way to get the exceptions expected by the test

Thank you in advance!

Solution

You look forward to javax persistence. PersistenceException

Just change the expected exception in the test

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>