當我使用JUnit的org.junit.rules.Timeout帶彈簧的基類AbstractTransactionalJUnit4SpringContextTests,我得到這個異常:使用JUnit 4的超時@rule與Spring的AbstractTransactionalJUnit4SpringContextTests
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
日誌輸出顯示:
2010-07-20 09:20:16 INFO [TransactionalTestExecutionListener.startNewTransaction] Began transaction (1): transaction manager [[email protected]]; rollback [true]
2010-07-20 09:20:16 INFO [TransactionalTestExecutionListener.endTransaction] Rolled back transaction after test execution for test context [[[email protected] testClass = MyIntegrationTest, locations = array<String>['classpath:/context.xml', 'classpath:/junit-context.xml'], testInstance = [email protected], testMethod = [email protected], testException = org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress]]
這裏是我的測試:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/context.xml", "classpath:/junit-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MyIntegrationTest extends AbstractTransactionalJUnit4SpringContextTests{
@Rule public Timeout globalTimeout = new Timeout(30000);
@Test
public void myTest() {
// transactional code here saving to the database...
}
}
然而,當我曾經評論過這個規則,它一切正常。
我該如何結婚這兩個一起正常工作?
我發現Timeout類啓動了一個新線程並在其中運行我的測試。這就是爲什麼我的交易得到創建,但我的代碼沒有包裹在它。現在要弄清楚如何讓它工作,我想如何... – JavaRocky 2010-07-20 08:55:23
日誌顯示正在設置和關閉的事務的原因是它生活在實際上計時器所在的原始線程上。而且可能是因爲事務使用了某種ThreadLocal實例,所以我的代碼沒有被包裝在事務中。 – JavaRocky 2010-07-20 12:03:52