如何使用 dbUnit 将数据库恢复到初始状态?

发布于 2024-09-25 21:22:44 字数 400 浏览 7 评论 0原文

我是自动化测试和 dbUnit 的新手。所以我很感激你的建议。

我将创建一个测试套件,它将按以下方式运行:

  • 创建一个内存 H2 数据库
  • 运行 DDL 脚本来创建表
  • 运行 dbUnit 来插入初始数据(我们称之为 STATE0)被所有测试使用。
  • 运行测试

直到那里对我来说看起来不错,但我不明白的是,在测试运行并更改数据后如何将数据库恢复到 STATE0

我可以用 dbUnit 来做吗?
还是用别的东西?
我应该在每次测试之前重新创建数据库吗?

在测试中简单地不提交事务对我来说并不合适,因为测试最终将在可能多个数据库连接上运行多个事务。

I am new to automated testing and dbUnit. So I would appreciate your advice.

I am going to create a test suite, that will run the following way:

  • create an in-memory H2 database
  • run DDL scripts to create tables
  • run dbUnit to insert initial data (let's call it STATE0) that will be used by all tests.
  • run tests

Till there it looks nice for me, but what I don't understand, is how do I revert the database to the STATE0 after a test run and changed the data?

Can I do it with dbUnit?
Or with something else?
Should I recreate the database before each test?

Simple not commiting transactions in tests is not appropriate for me, because the tests will eventually run more than one transaction over may be more than one database connection.

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

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

发布评论

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

评论(2

飘然心甜 2024-10-02 21:22:44

如果您正确编写 @BeforeClass@Before@After 方法,DBUnit 可以自动完成这四个工作。例如,在我们的项目中,使用 Derby,这样的测试用例如下所示:

public class MyTest {
    protected static IDataSet getDataSet() throws Exception {
        URL url = MyTest.class.getClassLoader().getResource("MyDataSet.xml");
        return new XmlDataSet(new FileInputStream(url.getPath()));
    }

    private static JdbcDatabaseTester databaseTester;

    @BeforeClass
    public static void setUpClass() throws Exception {
        // Init test environment, session etc.
        databaseTester = new JdbcDatabaseTester(
                "org.apache.derby.jdbc.ClientDriver",
                "jdbc:derby://localhost:1527/myschema", 
                "username", "password");
        databaseTester.setDataSet(getDataSet());
    }

    @AfterClass
    public static void tearDownClass() {
        // Close session etc.
    }

    @Before
    public void setUp() throws Exception {
        databaseTester.onSetup();
    }

    @After
    public void tearDown() throws Exception {
        databaseTester.onTearDown();
    }

    @Test
    public void test() throws Exception { ... }
}

此代码在每次测试后将数据库模式(的子集)恢复到由 MyDataSet.xml 定义的状态。 (请注意,正如 @Pascal 评论的那样,重置可能并不总是完整的 - 如果测试修改了不在数据集中的表,则它不会受到 @Before / @After 方法。)

DBUnit can do the work four you automatically if you write your @BeforeClass, @Before and @After methods properly. E.g. in our project, using Derby, one such test case looks like

public class MyTest {
    protected static IDataSet getDataSet() throws Exception {
        URL url = MyTest.class.getClassLoader().getResource("MyDataSet.xml");
        return new XmlDataSet(new FileInputStream(url.getPath()));
    }

    private static JdbcDatabaseTester databaseTester;

    @BeforeClass
    public static void setUpClass() throws Exception {
        // Init test environment, session etc.
        databaseTester = new JdbcDatabaseTester(
                "org.apache.derby.jdbc.ClientDriver",
                "jdbc:derby://localhost:1527/myschema", 
                "username", "password");
        databaseTester.setDataSet(getDataSet());
    }

    @AfterClass
    public static void tearDownClass() {
        // Close session etc.
    }

    @Before
    public void setUp() throws Exception {
        databaseTester.onSetup();
    }

    @After
    public void tearDown() throws Exception {
        databaseTester.onTearDown();
    }

    @Test
    public void test() throws Exception { ... }
}

This code puts back (a subset of) the DB schema to the state defined by MyDataSet.xml after each test. (Note that, as @Pascal commented, the reset may not always be full - if a test modifies a table which is not in the dataset, it won't be affected by the @Before / @After methods.)

在巴黎塔顶看东京樱花 2024-10-02 21:22:44

要将数据库初始化为初始数据集,只需在测试用例中实现这些方法:

@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
    return DatabaseOperation.CLEAN_INSERT; // by default (will do DELETE_ALL + INSERT)
}

@Override
protected DatabaseOperation getTearDownOperation() throws Exception
{
    return DatabaseOperation.NONE; // by default
}

如果某些测试在空表中插入行(例如未在初始数据集中定义),则可能有外键约束。

只需在数据集中添加这个没有任何行的空表:

<mydb_mypopulatedtable id="1" name="toto" alias="funky"/>
<mydb_mypopulatedtable id="2" name="titi" alias="groovy"/>
<mydb_mypopulatedtable id="3" name="tutu" alias="creepy"/>

<mydb_myemptytable />

这里,myemptytable 有一个指向 mypopulatedtable 的外键。如果未定义 myemptytable,DBUnit 将尝试删除 mypopulatedtable,但由于约束而失败。如果定义了,DBUnit 将删除之前的 myemptytable 行。

To initialize the database to the initial dataset, just implement these methods in your test case :

@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
    return DatabaseOperation.CLEAN_INSERT; // by default (will do DELETE_ALL + INSERT)
}

@Override
protected DatabaseOperation getTearDownOperation() throws Exception
{
    return DatabaseOperation.NONE; // by default
}

You may have foreign keys constraints if some of your tests insert rows in an empty table (not defined in initial dataset for example).

Just add this empty table in your dataset without any row :

<mydb_mypopulatedtable id="1" name="toto" alias="funky"/>
<mydb_mypopulatedtable id="2" name="titi" alias="groovy"/>
<mydb_mypopulatedtable id="3" name="tutu" alias="creepy"/>

<mydb_myemptytable />

Here, myemptytable has a foreign key to mypopulatedtable. If myemptytable was not defined, DBUnit would try to delete the mypopulatedtable but will fail because of the constraint. If defined, DBUnit will delete myemptytable rows before.

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