如何使用dbUnit将数据库恢复到初始状态? [英] How to revert the database back to the initial state using dbUnit?

查看:245
本文介绍了如何使用dbUnit将数据库恢复到初始状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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




  • 创建内存中的H2数据库

  • 运行DDL脚本以创建表

  • 运行dbUnit以插入初始值所有测试都将使用的数据(我们称之为 STATE0 )。

  • 运行测试



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



我可以使用dbUnit吗?

或其他什么东西?

我应该在每次测试之前重新创建数据库吗?



简单的不在测试中提交事务不适合我,因为测试最终会运行多个事务,可能会有多个数据库连接。

解决方案

如果您编写 @BeforeClass

DBUnit可以自动完成工作四, @Before @After 方法正确。例如。在我们的项目中,使用Derby,一个这样的测试用例看起来像

  public class MyTest {
protected static IDataSet getDataSet(抛出异常{
URL url = MyTest.class.getClassLoader()。getResource(MyDataSet.xml);
返回新的XmlDataSet(new FileInputStream(url.getPath()));
}

private static JdbcDatabaseTester databaseTester;

@BeforeClass
public static void setUpClass()throws Exception {
// Init测试环境,会话等
databaseTester = new JdbcDatabaseTester(
org.apache.derby.jdbc.ClientDriver,
jdbc:derby:// localhost:1527 / myschema,
username,password);
databaseTester.setDataSet(getDataSet());
}

@AfterClass
public static void tearDownClass(){
//关闭会话等
}

@
之前public void setUp()抛出异常{
databaseTester.onSetup();
}

@After
public void tearDown()抛出异常{
databaseTester.onTearDown();
}

@Test
public void test()抛出异常{...}
}

此代码将数据库模式(的一部分)放回到 MyDataSet.xml 定义的状态之后测试。 (注意,正如@Pascal评论的那样,重置可能并不总是满的 - 如果测试修改了不在数据集中的表,它将不会受 @Before / @After 方法。)


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.

解决方案

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.)

这篇关于如何使用dbUnit将数据库恢复到初始状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆