将 Junit 4 的超时 @Rule 与 Spring 的 AbstractTransactionalJUnit4SpringContextTests 结合使用
当我将 Junit 的 org.junit.rules.Timeout 与 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 [org.springframework.orm.jpa.JpaTransactionManager@6a1fbe]; rollback [true]
2010-07-20 09:20:16 INFO [TransactionalTestExecutionListener.endTransaction] Rolled back transaction after test execution for test context [[TestContext@17b60b6 testClass = MyIntegrationTest, locations = array<String>['classpath:/context.xml', 'classpath:/junit-context.xml'], testInstance = MyIntegrationTest@10a4d7c, testMethod = myTest@MyIntegrationTest, 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...
}
}
但是,每当我注释掉规则时,一切都正常。
我怎样才能将这两个结合在一起才能正常工作?
When i use Junit's org.junit.rules.Timeout with spring's base class AbstractTransactionalJUnit4SpringContextTests, i get this exception:
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
The log output shows:
2010-07-20 09:20:16 INFO [TransactionalTestExecutionListener.startNewTransaction] Began transaction (1): transaction manager [org.springframework.orm.jpa.JpaTransactionManager@6a1fbe]; rollback [true]
2010-07-20 09:20:16 INFO [TransactionalTestExecutionListener.endTransaction] Rolled back transaction after test execution for test context [[TestContext@17b60b6 testClass = MyIntegrationTest, locations = array<String>['classpath:/context.xml', 'classpath:/junit-context.xml'], testInstance = MyIntegrationTest@10a4d7c, testMethod = myTest@MyIntegrationTest, testException = org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress]]
Here is my test:
@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...
}
}
However whenever i comment out the rule, it all works fine.
How can i marry these two together to work correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,我解决了。我解决这个问题的方法是以编程方式设置交易。
希望有帮助。
Ahh, i worked it out. The way i solved it was to setup the transaction programatically.
Hope it helps.
哈哈。
您还可以简单地使用 @Transactional(timeout = 30) 注释您的测试方法,设置 30 秒的超时。这要简单得多。
LOL.
You can simply also annotate your test method with @Transactional(timeout = 30) for a 30 second timeout. Which is a lot simpler.