4

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?

user764974
  • 73
  • 1
  • 6

3 Answers3

2

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

user764974
  • 73
  • 1
  • 6
1

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

prusswan
  • 6,853
  • 4
  • 40
  • 61
  • Perhaps you can provide more concrete scenarios of what you want to do. For me, dbunit is used to prepare the test database states, while Unitils is used to manage and simplify the dbunit-specific configuration (like datasets for each test), that should work no matter JUnit or TestNg is used. If you are asking about using DbUnit with TestNG directly and nothing else, you might want to take note of this issues like: sourceforge.net/support/tracker.php?aid=1897627 and be prepared that DbUnit will be using JUnit internally for some things – prusswan Jul 12 '11 at 07:13
  • I need to make use of DbUnit in TestNG, since DbUnit extends JUnit i was not sure of how to go about this. Currently i am using IDatabaseTester instance instead of extending the class DbTestCase, is this the right way of using DbUnit in TestNG? – user764974 Jul 12 '11 at 16:59
  • I can't comment much on that since I do not use TestNg myself (and manage most of the dbunit configuration through Unitils), but after looking around I realize http://www.dbunit.org/howto.html#noextend already addresses the issue: you just need to find out what is the TestNg test class to extend, and the before and after methods for TestNg, and slot in the necessary wiring for IDatabaseTester (the example shown uses JdbcDatabaseTester) – prusswan Jul 12 '11 at 20:50
0

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
    }
}
Panu
  • 362
  • 3
  • 13