My understanding is that in the context of unit tests (TransactionalTestExecutionListener
), the code that otherwise looks up the transaction manager is not used (TransactionInterceptor#determineTransactionManager
).
You could try to annotate your test class with @TransactionConfiguration
, which accepts a transactionManager
attribute. Not the most elegant way, but possibly the best option for the time being.
地址:http://stackoverflow.com/questions/3027511/when-transactionmanager-is-not-named-transactionmanager
在我们要测试的类中引用@TransactionConfiguration,指向自己的事务管理就可以了。
全部代码如下:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:/xml/applicationContextTest.xml"}) @TransactionConfiguration(transactionManager = "tm_cmail_web") @Transactional public class SysModelTest { @Resource private WebSysModelService modelService; @Test @Transactional @Rollback(true) public void testCreate(){ WebSysModel m=new WebSysModel(); m.setModelMid("HelloDing"); m.setModelName("ding"); m.setCreateTime(Calendar.getInstance().getTime()); m.setModfiyTime(Calendar.getInstance().getTime()); short s=1; m.setModelType(s); m.setModelUrlType(s); modelService.create(m); } }