如果想隱藏一些測試流程的細節,例如IDatabaseConnection等的建立,則可以繼承 DatabaseTestCase 的相關子類別,DatabaseTestCase繼承自TestCase,所以你必須使用JUnit 3.x的程式庫,DatabaseTestCase之類別是 DBTestCase,通常會繼承這個類別,或繼承DBTestCase的相關子類別,如 DataSourceBasedDBTestCase、JdbcBasedDBTestCase、JndiBasedDBTestCase。這些類別都是抽象類別,繼承後都有其規定要實作的方法。
以 使用 DbUnit 中第二個例子而言,如果改繼承 DataSourceBasedDBTestCase 的方式,則必須實作getDataSource()與getDataSet()方法。如下所示:
package test.cc.openhome;
import static org.dbunit.Assertion.assertEqualsIgnoreCols;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.dbunit.DataSourceBasedDBTestCase;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import cc.openhome.dao.BookmarkDAO;
import cc.openhome.dao.BookmarkDAOImpl;
import cc.openhome.model.Bookmark;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class BookmarkDAOImplTest extends DataSourceBasedDBTestCase {
    private BookmarkDAO dao;
    
    @Override
    protected DataSource getDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/exercise");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        return dataSource;
    }
    @Override
    protected IDataSet getDataSet() throws Exception {
        return getXMLDataSet("dataset.xml");
    }
    private IDataSet getXMLDataSet(String file) throws Exception {
        return new FlatXmlDataSetBuilder()
                       .build(new FileInputStream(file));
    }
    public void setUp() throws Exception {
        // 記得呼叫父類別setUp(),預設行為是CLEAN_INSERT
        super.setUp();
        dao = new BookmarkDAOImpl(getDataSource());
    }
    
    public void testGet() throws Exception {
        // 用待測的DAO讀取資料
        List<Bookmark> result = dao.get();
        // 用 DbUnit 讀取資料
        IDataSet dataSet = 
            getDatabaseTester().getConnection().createDataSet();
        // 取得表格資料
        ITable table = dataSet.getTable("T_BOOKMARK");
        List<Bookmark> expected = new ArrayList<Bookmark>();
        // 表格中的列(row)數
        int rows = table.getRowCount();
        for(int i = 0; i < rows; i++) {
            // 取得每列的各個欄位
            String url = (String) table.getValue(i, "url");
            String title = (String) table.getValue(i, "title");
            String category = (String) table.getValue(i, "category");
            expected.add(new Bookmark(url, title, category));
        }
        // 斷言相等性
        assertEquals(expected, result);
    }
    
    public void testAdd() throws Exception {
        // 用待測的DAO安插資料
        Bookmark bookmark = new Bookmark("http://m", "n", "o");
        dao.add(bookmark);
        // 用 DbUnit 取得資料
        IDataSet dataSet = 
            getDatabaseTester().getConnection().createDataSet();
        // 讀取預期資料集合
        IDataSet expected = getXMLDataSet("expected.xml");
        // 斷言資料集合相等性,但忽略id欄位
        assertEqualsIgnoreCols(expected, dataSet, 
                "T_BOOKMARK", new String[] {"id"});
    }
}

