我正在使用 TestNG 使用 AbstractTransactionalTestNGSpringContextTests 作为基类来测试持久性 Spring 模块(JPA+Hibernate)。@Autowired、@TransactionConfiguration、@Transactional 的所有重要部分都可以正常工作。
当我尝试使用 threadPoolSize=x, invocationCount=y TestNG 注释在并行线程中运行测试时,问题就出现了。
WARNING: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@174202a]
to process 'before' execution of test method [testCreate()] for test instance [DaoTest] java.lang.IllegalStateException:
Cannot start new transaction without ending existing transaction: Invoke endTransaction() before startNewTransaction().
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:123)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:374)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:146)
...有人遇到过这个问题吗?
这是代码:
@TransactionConfiguration(defaultRollback = false)
@ContextConfiguration(locations = { "/META-INF/app.xml" })
public class DaoTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
private DaoMgr dm;
@Test(threadPoolSize=5, invocationCount=10)
public void testCreate() {
...
dao.persist(o);
...
}
...
更新:似乎 AbstractTransactionalTestNGSpringContextTests 仅在所有其他测试线程都没有获得自己的事务实例时才为主线程维护事务。解决这个问题的唯一方法是扩展 AbstractTestNGSpringContextTests 并以编程方式维护事务(而不是 @Transactional 注释)每个方法(即使用 TransactionTemplate):
@Test(threadPoolSize=5, invocationCount=10)
public void testMethod() {
new TransactionTemplate(txManager).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// transactional test logic goes here
}
}
}