Failed to retrieve PlatformTransactionManager for @Transactional test for test context

前端 未结 1 1112
不知归路
不知归路 2021-01-11 19:59

Whhen trying to test caching capabilities of Hibernate\'s (version 4) EHCache between transactions - it fails: Failed to retrieve PlatformTransactionManager for @Trans

相关标签:
1条回答
  • 2021-01-11 20:09

    @Transactional requires a bean with name transactionManager in your application context if not specified explictly. Specify the transaction manager you want to use with your test using the @Transaction annotation value attribute

    For example if you want to use hibernateTransactionManager specify this as

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = { ApplicationConfig.class, CachingConfig.class }, loader = AnnotationConfigContextLoader.class)
    @PersistenceContext
    @Transactional("hibernateTransactionManager")
    public class EHCacheTest extends AbstractTransactionalJUnit4SpringContextTests {
    }
    

    Otherwise rename the transaction manager you want to use to have the default name transactionManager

    @Bean
        public PlatformTransactionManager transactionManager() { // TODO: Really need this?
            final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
            transactionManager.setSessionFactory(sessionFactory().getObject());
            return transactionManager;
        }
    
    0 讨论(0)
提交回复
热议问题