Here is a 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 } }
source share