简体   繁体   English

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

[英]How to revert the database back to the initial state using dbUnit?

I am new to automated testing and dbUnit. 我是自动化测试和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 创建一个内存H2数据库
  • run DDL scripts to create tables 运行DDL脚本来创建表
  • run dbUnit to insert initial data (let's call it STATE0 ) that will be used by all tests. 运行dbUnit以插入将由所有测试使用的初始数据(让我们称之为STATE0 )。
  • 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? 直到那里它看起来不错,但我不明白的是,如何在测试运行后将数据库恢复到STATE0并更改数据?

Can I do it with dbUnit? 我可以用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. 简单的不在测试中提交事务对我来说是不合适的,因为测试最终会运行多个事务,可能不止一个数据库连接。

DBUnit can do the work four you automatically if you write your @BeforeClass , @Before and @After methods properly. DBUnit的可以,如果你写你自动完成的工作相关的四个你@BeforeClass@Before@After方法正确。 Eg in our project, using Derby, one such test case looks like 例如,在我们的项目中,使用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 { ... }
}

This code puts back (a subset of) the DB schema to the state defined by MyDataSet.xml after each test. 此代码在每次测试后将数据库模式的(子集)放回到MyDataSet.xml定义的状态。 (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.) (注意,正如@Pascal评论的那样,重置可能并不总是满的 - 如果测试修改了不在数据集中的表,它将不受@Before / @After方法的影响。)

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. 在这里,myemptytable有mypopulatedtable的外键。 If myemptytable was not defined, DBUnit would try to delete the mypopulatedtable but will fail because of the constraint. 如果未定义myemptytable,DBUnit将尝试删除mypopulatedtable但由于约束而失败。 If defined, DBUnit will delete myemptytable rows before. 如果已定义,DBUnit将删除myemptytable行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM