I’m using Spring 3.1.2 with Hibernate 4.
I have a DAO implementation class MyDaoImpl annotated with @Repository so that exception translation is enabled. I have a service class MyService annotated with @Transactional as follows:
public class MyService implements IMyService {
private MyDao myDao;
@Autowired
public void setMyDao(MyDao dao) {
this.myDao = dao;
}
@Override
@Transactional
public void createA(String name)
{
A newA = new A(name);
this.myDao.saveA(newA);
}
}
I’ve wrote a unit tests class MyServiceTest as follows:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:beans.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class MyServiceTest implements IMyServiceTest {
private IMyService myService;
private SessionFactory sessionFactory;
@Autowired
public void setMyService(IMyService myService)
{
this.myService = myService;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Test
@Override
public void testCreateA()
{
//Assume that there is already a row of table A with the name "A1"
//so I expect to get a Spring DataAccessException (or subtypes)
//when the session is flushed
this.myService.createA("A1");
this.sessionFactory.getCurrentSession().flush();
//asserts
}
}
When I run the test, I get a Hibernate specific exception ConstraintViolationException. I’ve found on the forum that this is because the translation system takes place outside the transaction, so in this case after testCreateA() returns. I don’t know if this is the real cause, but if it is, it means that I can’t test that the translation works for my DAOs. One solution would be to remove the @Transactional annotations from my unit tests, but I would no benefit from the rollback feature.
What are your recommendations?
EDIT: I’ve added the SessionFactory declared in my context to the test class, so that I can access the current session for flushing.
Some additional explanations: In this case, I get the exception when the session is flushed (which is inside the transaction). I flush the session in order to avoid false positives as it is explained in the docs. Also, since the default propagation is REQUIRED, the testCreateA() transaction is also used for the call to createA(), so the changes are not flushed (generally) until testCreateA() returns.
Have you added
PersistenceExceptionTranslationPostProcessorbean defination? LikeFrom Spring doc.