要将数据库初始化为初始数据集,只需在测试用例中实现这些方法:
@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.CLEAN_INSERT; // by default (will do DELETE_ALL + INSERT)
}
@Override
protected DatabaseOperation getTearDownOperation() throws Exception
{
return DatabaseOperation.NONE; // by default
}
如果您的某些测试在空表中插入行(例如,未在初始数据集中定义),您可能会有外键约束。
只需在您的数据集中添加此空表,无需任何行:
<mydb_mypopulatedtable id="1" name="toto" alias="funky"/>
<mydb_mypopulatedtable id="2" name="titi" alias="groovy"/>
<mydb_mypopulatedtable id="3" name="tutu" alias="creepy"/>
<mydb_myemptytable />
这里,myemptytable 有一个指向 mypopulatedtable 的外键。如果未定义 myemptytable,DBUnit 将尝试删除 mypopulatedtable,但由于约束而失败。如果已定义,DBUnit 将删除之前的 myemptytable 行。