@Before 和 @After 排除

发布于 2024-09-29 22:39:15 字数 655 浏览 4 评论 0原文

我正在为我的 DAO 编写一些测试,因为很多测试使用保存到我的数据库中的测试对象,所以我创建了一个 setup() 和teardown() 方法,分别带有注释 @Before 和 @After为了避免冗余代码,但其中一个测试(实际上并不真正需要测试对象)调用 DAO 中的一个方法,其中包含调用 getCurrentSession().clear() (这是一种使用 ScrollableResults 获取数据的方法批量从数据库中读取,为了避免内存被填满,它每 50 行调用一次lush()和clear()。这会产生一个问题,因为clear()实际上从会话中删除了在setup()中创建的测试对象,所以当调用teardown()时我得到一个错误:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [nl.ru.cmbi.pdbeter.core.model.domain.PDBEntry#395]

Is there a way to sell JUnit to not use the setup () 和teardown() 在此测试中,还是将所有实际上不需要setup() 和teardown() 的测试放在一个新的测试类中更好?

I'm writing some tests for my DAO, and because a lot of the tests use a test object that is being saved to my database I've created a setup() and teardown() method with the annotations @Before and @After respectively to avoid redundant code, but one of the tests, actually one that doesn't really need the test object, calls a method in the DAO that contains the call getCurrentSession().clear() (it's a method that uses ScrollableResults to get data from the db in batches, and to avoid the memory to fill up it calls flush() and clear() every 50 rows). This creates a problem, because the clear() actually removes the test object that is created in setup() from the session, so when teardown() is called I get an error:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [nl.ru.cmbi.pdbeter.core.model.domain.PDBEntry#395]

Is there a way to tell JUnit to not use the setup() and teardown() on this test, or is it better to put all the tests that don't actually need the setup() and teardown() in a new test class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

孤独患者 2024-10-06 22:39:16

首先,是的,在单独的测试中隔离不需要 @Before@After 行为的测试非常有意义。

其次,您可能想看看 Spring 框架支持在数据库事务中运行单元测试,该事务会在每次测试结束时自动回滚,这样您就不必担心某些测试会影响数据库的状态外部资源,或者询问每个订单正在运行哪些测试等问题。将其与内存数据库结合起来(例如 HSQL< /a>),您甚至不需要担心在某个地方有一个正在运行的数据库来运行,这使得您的构建更加可移植。

First of all yes, it makes a lot of sense to isolate tests that don't need the @Before or @After behavior in a separate test.

Secondly, you may want to take a look at the Spring Framework's support for running unit tests within a database transaction that is automatically rolled back at the end of each test, so that you do not have to ever worry about some tests affecting the state of an external resource, or ask questions about which tests are being run in each order, etc. Combine this with an in-memory database (like HSQL) and you won't even need to worry about having a running database somewhere to run against, making your build much more portable.

旧夏天 2024-10-06 22:39:16

JUnit 将为每个测试运行所有用 @Before@After 注释的方法,因此您需要将测试分成两个类。

JUnit will run all methods annotated with @Before and @After for each test so you will need to split your tests into two classes.

梦中的蝴蝶 2024-10-06 22:39:16

定义您自己的 BlockJUnit4ClassRunner:

public class WithoutBeforeAfter extends BlockJUnit4ClassRunner {

    public WithoutBeforeAfter(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
        if(method.getName().equals("methodNameIgnore@Before")){
            return statement;
        }else{
            return super.withBefores(method, target, statement);
        }
    }

    @Override
    protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
        if(method.getName().equals("methodNameIgnore@After")){
            return statement;
        }else{
            return super.withAfters(method, target, statement);
        }
    }
}

然后在您的测试用例中使用它:

@RunWith(value=WithoutBeforeAfter.class)

至少它在 JUnit 4.8 中可用。

Define your own BlockJUnit4ClassRunner:

public class WithoutBeforeAfter extends BlockJUnit4ClassRunner {

    public WithoutBeforeAfter(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
        if(method.getName().equals("methodNameIgnore@Before")){
            return statement;
        }else{
            return super.withBefores(method, target, statement);
        }
    }

    @Override
    protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
        if(method.getName().equals("methodNameIgnore@After")){
            return statement;
        }else{
            return super.withAfters(method, target, statement);
        }
    }
}

Then use it in your test case:

@RunWith(value=WithoutBeforeAfter.class)

At least it is available in JUnit 4.8.

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