单元测试 PersistenceLayer (JPA/EclipseLink)

发布于 2024-08-01 16:03:15 字数 1478 浏览 2 评论 0原文

如果没有准备好的内存数据库,我不确定测试数据库的正确方法是什么。 我有以下 JUnit4 的 TestSuite。

忽略 JpaManager,我只需要这个,因为应用程序作为 Eclipse RCP 运行,而不是在容器中运行,也没有使用 Spring(尚未),所以我无法注入 EntityManager 引用,必须手动处理它。< /em>

public class CustomerJpaTest {

    private Customer testCustomer;

    @Before
    public void setUp() throws Exception {
        JpaManager.getInstance().begin();
        // create a new user for testing
        CustomerJpaDao dao = new CustomerJpaDao();
        testCustomer = new Customer();
        testCustomer.setName("Someone");
        dao.persist(testCustomer);
        JpaManager.getInstance().commit();
    }

    @After
    public void tearDown() throws Exception {
        // remove previously created user
        CustomerJpaDao dao = new CustomerJpaDao();
        dao.remove(testCustomer);
        JpaManager.getInstance().commit();
        JpaManager.getInstance().dispose();
    }

    @Test
    public void testCustomerSaving() throws Exception {
        // not sure yet
    }

    @Test
    public void testCustomerLoading() throws Exception {
        ICustomerDao dao = new CustomerJpaDao();
        Customer customer = dao.findByName("Someone");
        assertEquals("Someone", customer.getName());
    }
}

由于我在真实的数据库上运行,因此我正在创建我将在 setUp 方法中测试的对象,并在测试完成后将其删除。 这正是我的问题:这个setUp和tearDown实际上也可能是某种测试(testCustomerSaving,testCustomerDelete),但是当我以特定顺序运行测试时,它们不会被隔离(当保存失败,加载也失败,然后删除)。

这样做的正确方法是什么?

I am not sure what is the correct way of testing my Database, without an prepared In-Memory-Database. I have following TestSuite for JUnit4.

Ignore the JpaManager, I just need this, because the application runs as a Eclipse RCP and not in a container, nor Spring is used (yet), so I cannot inject an EntityManager-reference and have to handle it manually.

public class CustomerJpaTest {

    private Customer testCustomer;

    @Before
    public void setUp() throws Exception {
        JpaManager.getInstance().begin();
        // create a new user for testing
        CustomerJpaDao dao = new CustomerJpaDao();
        testCustomer = new Customer();
        testCustomer.setName("Someone");
        dao.persist(testCustomer);
        JpaManager.getInstance().commit();
    }

    @After
    public void tearDown() throws Exception {
        // remove previously created user
        CustomerJpaDao dao = new CustomerJpaDao();
        dao.remove(testCustomer);
        JpaManager.getInstance().commit();
        JpaManager.getInstance().dispose();
    }

    @Test
    public void testCustomerSaving() throws Exception {
        // not sure yet
    }

    @Test
    public void testCustomerLoading() throws Exception {
        ICustomerDao dao = new CustomerJpaDao();
        Customer customer = dao.findByName("Someone");
        assertEquals("Someone", customer.getName());
    }
}

Since I am running on a real database, I am creating my object that I am going to test in the setUp method and remove it after the test(s) are done. And exactly here is my problem: this setUp and tearDown could be actually also some kind of tests (testCustomerSaving, testCustomerDelete), but when I have the tests running in a particular order then they wont be isolated (when saving fails, loading fails as well, and then deleting).

What is the right way to do this?

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

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

发布评论

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

评论(1

娇柔作态 2024-08-08 16:03:15

setUp 中开始事务并在 tearDown 中回滚事务。

Begin a transaction in setUp and rollback the transaction in tearDown.

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