user764974
user764974

Reputation: 73

how to use DbUnit with TestNG

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?

Upvotes: 4

Views: 4434

Answers (3)

Panu
Panu

Reputation: 362

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

Upvotes: 0

user764974
user764974

Reputation: 73

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

Upvotes: 2

prusswan
prusswan

Reputation: 7091

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

Upvotes: 1

Related Questions