Dbunit-Intro: Commons IO. Eclipse 3.1.x
Dbunit-Intro: Commons IO. Eclipse 3.1.x
Dbunit-Intro: Commons IO. Eclipse 3.1.x
Another options is to download the DbUnit jar manually to the DbUnit libs plugin directory.
We will need to copy the file to libs/dbunit-2.2.jar, libs/dbunit-2.2-javadoc.jar and libs/dbunit-2.2-
sources.jar.
DBUNIT LIFECYCLE
DbUnit framework follows some steps in its life cycle :
1. Removing the old data left in the database from previous tests.
2. Loading some data from XML file into the database.
3. Running the test.
DatabaseTestCase class provides two methods setUp() and TearDown() which internally call
getSetUpOperation() and getTearDownOperation() methods respectively. setUp() method
provides the setup operation DatabaseOperation.CLEAN_INSERT or
DatabaseOperation.REFRESH. DatabaseOperation.CLEAN_INSERT operation is the
combination of two operations DELETE_ALL and INSERT. So data defined in the XML file is
loaded in the database by this operation. First two steps of the life cycle are executed when
executing the setUp() method before running the test. These steps allow you not to waste time in
writing code to restore state in the database. DatabaseOperation.REFRESH updates the desired
database with the data found in the XML file. The getTearDownOperation() performs a NONE
operation which does nothing.
protected void setUp() throws Exception{
super.setUp();
executeOperation(getSetUpOperation());
}
protected void tearDown() throws Exception{
super.tearDown();
executeOperation(getTearDownOperation());
}
protected DatabaseOperation getSetUpOperation() throws Exception{
return DatabaseOperation.CLEAN_INSERT;
}
protected DatabaseOperation getTearDownOperation() throws Exception{
return DatabaseOperation.NONE;
}
DbUnit can work with default behavior, however, you can override the methods according to the s
requirement.
3.
Because of its being an abstract class we need to implement these two methods:
TestDbUnit.java :
...........................
...........................
// Provide a connection to the database
protected IDatabaseConnection getConnection() throws Exception{
Class driverClass = Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection =
DriverManager.getConnection("jdbc:mysql://localhost:
3306/hrapptest", "root", "root");
return new DatabaseConnection(jdbcConnection);
}
// Load the data which will be inserted for the test
protected IDataSet getDataSet() throws Exception{
loadedDataSet =
new FlatXmlDataSet(this.getClass().getClassLoader().
getResourceAsStream("input.xml"));
return loadedDataSet;
}
............................
............................
4.
Writing Test :
Now, write test to check that the data has been loaded in TestDbUnit.java file:
............................
............................
public void testCheckLoginDataLoaded() throws Exception{
assertNotNull(loadedDataSet);
int rowCount = loadedDataSet.getTable(TABLE_LOGIN).getRowCount();
assertEquals(2, rowCount);
}
............................
............................
Combining all of the above functionalities into one TestDbUnit.java file, you will find it as follows :
TestDbUnit.java :
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
public class TestDbUnit extends DatabaseTestCase{
public static final String TABLE_LOGIN = "login";
private FlatXmlDataSet loadedDataSet;
Running Test :
Now, in Eclipse, go to Run->Run As and click "JUnit Test" to run tests. If testing is successful
then a green strip appears at the left of the eclipse window. If any of the test fails then it turns into
a red strip indicating failure of any test.