I'm trying to understand why I cannot rollback transaction if I throw exception in my test?
I'm using Spring 4.1.5 and I'm trying to test my transactions. I've annotated @Transactional my repository and transaction have been rolled back if repository throw exception. Also I've annotated @Transactional my test method and calling several methods from repository work in one transaction. However when I trow exception in test by myself transaction is not rolled back. Why? It looks like it was done by some purpose or do I do something wrong?
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@ContextHierarchy({
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/jpa-persistence-context.xml"})
})
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
public class FeaturedGroupRepositoryTest2 {
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
@Test
public void testFeaturedGroupDao() {
FeaturedGroupEntity newFeaturedGroupEntity = new FeaturedGroupEntity();
FeaturedGroupEntity savedFeaturedGroupEntity = featuredGroupRepository.save(newFeaturedGroupEntity);
FeaturedGroupEntity foundFeaturedGroupEntity = featuredGroupRepository.findOne(savedFeaturedGroupEntity.getId());
throw new RuntimeException("test rollback");
}
}