在JUnit中测试REQUIRES

编程入门 行业动态 更新时间:2024-10-26 09:28:27
在JUnit中测试REQUIRES_NEW(Testing REQUIRES_NEW in JUnit)

运行我的测试时,它会在调用method()时挂起。 难道我做错了什么? 帮帮我!

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"beans.xml"}) @Transactional(rollbackFor = Exception.class) public class Test { @Test public void test() { itemUnderTestDao.method(); // ... } } public class ItemUnderTestDao { @Transactional(propagation = Propagation.REQUIRES_NEW) public void method() { // ... } }

忘记上面的代码。 我现在已经在http://www44.zippyshare.com/v/46865082/file.html上传了一个Eclipse项目(不包括像spring这样的依赖项)。 关于如何让测试通过的所有输入都非常感谢! 如果我有两个sqlite数据库和两个dataSources,测试通过...

When running my test, it hangs at the call to method(). Am I doing something wrong? Help!

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"beans.xml"}) @Transactional(rollbackFor = Exception.class) public class Test { @Test public void test() { itemUnderTestDao.method(); // ... } } public class ItemUnderTestDao { @Transactional(propagation = Propagation.REQUIRES_NEW) public void method() { // ... } }

Forget the above code. I have now uploaded an Eclipse project (excluding dependencies like spring) at http://www44.zippyshare.com/v/46865082/file.html. All input on how to get the test to pass is really appreciated! The test pass if I have two sqlite databases and two dataSources...

最满意答案

正如mrembisz所说,你正试图同时获得2个连接/线程。

对于测试本身,其将在测试期间存在 对于方法调用,它将在调用期间存在。

我在一些不同的情况下遇到了同样的事情。

有两种方法可以解决这个问题:

将传播更改为REQUIRED ,这样您将重用最初获取的测试的Connection。 增加池大小,但请记住,您需要考虑同时更新的最大数量,大致maxThreadPoolSize >= (maxSimultaniousUpdates + 1) + (min size for other activities) (如果您在列表1挂起连接,最终所有挂起操作将完成)。 Othrewise这将在生产中重新出现。

第一个解决方案对我来说似乎更好,当然如果它不会损害你的逻辑:)

As mrembisz said you are trying to get 2 connections at the same time/thread.

For test itself, which would exist for a duration of the test For a method call, which would exist for duration of the call.

I encountered the same thing in a little bit different circumstances.

2 ways you can fix this:

Change propagation to REQUIRED, this way you would reuse Connection that test originally acquired. Increase pool size, but keep in mind, that you need to consider max number of simultanious updates, roughly maxThreadPoolSize >= (maxSimultaniousUpdates + 1) + (min size for other activities) (If you have at list 1 pending connection, eventually all pending operations would complete). Othrewise this would reappear in production.

First solution seems to me better one, ofcourse if it does not harm your logic :)

更多推荐

本文发布于:2023-07-30 06:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   JUnit   REQUIRES

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!