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 {
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!
source
share