如何将 DbUnit 与 TestNG 结合使用

发布于 2024-11-19 09:41:32 字数 112 浏览 2 评论 0原文

我需要将 DbUnit 与 TestNG 集成。

1) 是否可以将 DbUnit 与 TestNG 一起使用,因为 DbUnit 基本上是 JUnit 的扩展。
2)如果是的话怎么办?

I need to integrate DbUnit with TestNG.

1) Is it possible to use DbUnit with TestNG as DbUnit is basically an extension of JUnit.
2) If yes how?

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

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

发布评论

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

评论(3

装纯掩盖桑 2024-11-26 09:41:32

最后我找到了一种将 DbUnit 与 TestNG 结合使用的方法!

使用 IDatabaseTester 实例是可行的,

但另一种解决方法是:
扩展 AbstractDatabaseTester 并实现 getConnection 并重写必要的函数。
但一件重要的事情是在测试之前和之后调用 onSetup() 和 onTeardown()。

希望这有帮助...

Finally i found out a way to use DbUnit with TestNG!

Using Instance of IDatabaseTester works,

but another work around would be :
To extend AbstractDatabaseTester and implement getConnection and override necessary functions.
But one important thing is to call onSetup() and onTeardown() before and after testing.

Hope this helps...

a√萤火虫的光℡ 2024-11-26 09:41:32

不确定您到底想做什么,但也许 Unitils 会有所帮助。它类似于 dbunit 扩展,但不限于此,并且支持与 TestNg 集成(通过为您的测试用例扩展 UnitilsTestNG 类)。

Not sure what you are trying to do exactly, but perhaps Unitils would be helpful. It is like a dbunit extension but not limited to that, and supports integration with TestNg (by extending UnitilsTestNG class for your testcase).

驱逐舰岛风号 2024-11-26 09:41:32

这是执行所需功能的简单类。

public class SampleDBUnitTest {

    IDatabaseTester databaseTester;
    IDataSet dataSet;

    @BeforeMethod
    public void setUp() throws Exception {
        // These could come as parematers from TestNG 
        final String driverClass = "org.postgresql.Driver";
        final String databaseUrl = "jdbc:postgresql://localhost:5432/database";
        final String username = "username";
        final String password = "password";

        dataSet = new FlatXmlDataSet(Thread.currentThread().getContextClassLoader().getResourceAsStream("dataset.xml"));
        databaseTester = new JdbcDatabaseTester(driverClass, databaseUrl, username, password);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setDataSet(dataSet);
        databaseTester.setTearDownOperation(DatabaseOperation.NONE);
        databaseTester.setDataSet(dataSet);

        databaseTester.onSetup();
    }

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

    @Test
    public void t() throws Exception {
        // Testing, testing
    }
}

Here is simple class that performs the required function.

public class SampleDBUnitTest {

    IDatabaseTester databaseTester;
    IDataSet dataSet;

    @BeforeMethod
    public void setUp() throws Exception {
        // These could come as parematers from TestNG 
        final String driverClass = "org.postgresql.Driver";
        final String databaseUrl = "jdbc:postgresql://localhost:5432/database";
        final String username = "username";
        final String password = "password";

        dataSet = new FlatXmlDataSet(Thread.currentThread().getContextClassLoader().getResourceAsStream("dataset.xml"));
        databaseTester = new JdbcDatabaseTester(driverClass, databaseUrl, username, password);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setDataSet(dataSet);
        databaseTester.setTearDownOperation(DatabaseOperation.NONE);
        databaseTester.setDataSet(dataSet);

        databaseTester.onSetup();
    }

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

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