将 Junit 4 的超时 @Rule 与 Spring 的 AbstractTransactionalJUnit4SpringContextTests 结合使用

发布于 2024-09-10 09:32:57 字数 1742 浏览 1 评论 0原文

当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-09-17 09:32:57

啊,我解决了。我解决这个问题的方法是以编程方式设置交易。

@Autowired TransactionManager transactionManager;

@Test
public void test() {     
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            status.setRollbackOnly();

            // DO YOUR TEST LOGIC HERE
        }
     });
}

希望有帮助。

Ahh, i worked it out. The way i solved it was to setup the transaction programatically.

@Autowired TransactionManager transactionManager;

@Test
public void test() {     
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            status.setRollbackOnly();

            // DO YOUR TEST LOGIC HERE
        }
     });
}

Hope it helps.

故乡的云 2024-09-17 09:32:57

哈哈。

您还可以简单地使用 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文