Hibernate 单元测试 - 重置架构

发布于 2024-08-31 01:16:46 字数 277 浏览 5 评论 0原文

我正在 JUnit 测试中测试 DAO 的 CRUD 操作。 当我执行单个测试时,Hibernate 总是重置架构并以已知状态填充数据库。但是,当我在中执行多个测试时,Hibernate会重置架构一次,然后在测试执行期间累积数据。

这是意外的行为,因此我想在测试的 @Before 方法中添加一个显式重置架构的函数,以避免执行链期间先前测试创建的辅助数据持续存在。

有什么建议吗?

谢谢

I'm testing the CRUD operations of my DAOs in JUnit tests.
When i execute the single test, Hibernate always resets the schema and populates the DB in a known state. But when i execute multiple tests in a row, Hibernate resets the schema once, and then the data is accumulated during the execution of the tests.

This is an unexpected behavior, so I'd like to add in the @Before method of the tests a function that explicitly resets the schema to avoid the pesistence of side data created by previous tests during the execution chain.

Any tips?

Thanks

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

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

发布评论

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

评论(2

安静被遗忘 2024-09-07 01:16:46

首先,您所做的不是单元测试,而是集成测试。

Tips:

  • 使用事务并回滚。
  • 考虑使用 DBUnit(不确定它对您的情况有多大帮助)。
  • 以下是来自 org.springframework.orm.hibernate3.LocalSessionFactoryBean 的一些相关代码。

...

Connection con = session.connection();
Dialect dialect = Dialect.getDialect(getConfiguration().getProperties());
String[] sql = getConfiguration().generateSchemaCreationScript(dialect);
executeSchemaScript(con, sql);

First of all, what you're doing is not unit testing, it's integration testiong.

Tips:

  • Use transactions and roll the back.
  • Consider using DBUnit (not sure how helpful it would be in your case).
  • Here's some relevant code from org.springframework.orm.hibernate3.LocalSessionFactoryBean.

...

Connection con = session.connection();
Dialect dialect = Dialect.getDialect(getConfiguration().getProperties());
String[] sql = getConfiguration().generateSchemaCreationScript(dialect);
executeSchemaScript(con, sql);
浊酒尽余欢 2024-09-07 01:16:46

您可以在每个 @Test 方法之前开始一个事务并回滚该事务,如下所示:

public class HibernateIntegrationTest {
    protected static SessionFactory factory;
    protected Session session;

    @BeforeClass
    public static void createSessionFactory() {
        AnnotationConfiguration config = new AnnotationConfiguration();
        // ...
        config.configure();
        factory = config.buildSessionFactory();
    }    
    @AfterClass
    public static void closeSessionFactory() {
        if (factory != null) {
            factory.close();
        }
    }
    @Before
    public void beginTransaction() {
        session = factory.getCurrentSession();
        session.beginTransaction();
    }
    @After
    public void rollbackTransaction() {    
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
}

并在您的测试类中扩展此类。

public class DemoTest extends HibernateIntegrationTest {
    @Test
    public void createMyEntity() {
        MyEntity e = new MyEntity();
        //...
        session.save(e);

        assertNotNull(e.getId());
    }
}

请注意,这不是最干净的方法,上面的代码更多用于演示目的。

You could begin a transaction before and rollback the transaction each @Test method, something like this:

public class HibernateIntegrationTest {
    protected static SessionFactory factory;
    protected Session session;

    @BeforeClass
    public static void createSessionFactory() {
        AnnotationConfiguration config = new AnnotationConfiguration();
        // ...
        config.configure();
        factory = config.buildSessionFactory();
    }    
    @AfterClass
    public static void closeSessionFactory() {
        if (factory != null) {
            factory.close();
        }
    }
    @Before
    public void beginTransaction() {
        session = factory.getCurrentSession();
        session.beginTransaction();
    }
    @After
    public void rollbackTransaction() {    
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
}

And extend this class in your tests classes.

public class DemoTest extends HibernateIntegrationTest {
    @Test
    public void createMyEntity() {
        MyEntity e = new MyEntity();
        //...
        session.save(e);

        assertNotNull(e.getId());
    }
}

Note that this is not the cleanest way to do this, the code above is more for demonstration purposes.

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