我写了一个单元测试来测试DAO的保存方法,我确信我的保存方法有效,但是,当我运行单元测试时,它遇到了这个错误:
junit.framework.ComparisonFailure: row count (table=RECONCILIATION_OBJECT_HANDLER) expected:<[4]> but was:<[3]>
这是我的单元测试代码:
@Test
@Transactional
@Rollback(true)
@ExpectedDatabase(value="classpath:/expectedTable/expectedReconciliationHandler.xml",table="RECONCILIATION_OBJECT_HANDLER")
public void testsaveSingleHandler() {
ReconciliationObjectHandler handler = new ReconciliationObjectHandler();
handler.setObjectName("employee");
handler.setHandler("NewHandler");
dao.saveSingleHandler(handler);
}
这是我的预期结果:
<RECONCILIATION_OBJECT_HANDLER id="0"
OBJECT_NAME="employee"
HANDLER="FieldNameHandler" />
<RECONCILIATION_OBJECT_HANDLER id="1"
OBJECT_NAME="employee"
HANDLER="PickListHandler" />
<RECONCILIATION_OBJECT_HANDLER id="2"
OBJECT_NAME="employee"
HANDLER="SimpleHandler" />
<RECONCILIATION_OBJECT_HANDLER id="3"
OBJECT_NAME="employee"
HANDLER="NewHandler" />
-----------------更新--------------------------------------------
我意识到我不需要添加回滚注释,我需要提供所有的列信息来进行结果比较,然而,每次我运行单元测试时,INSERT操作都会使列"id"自动递增,导致"id"字段与xml文件中的预期结果不匹配,我怎么能忽略比较列"id"?现在错误信息是:
junit.framework.ComparisonFailure: value (table=RECONCILIATION_OBJECT_HANDLER, row=3, col=id) expected:<[3]> but was:<[12]>
我通过阅读文档找到了解决方案:弹簧测试DBUnit
我可以在注释@ExpectedDatabase中选择assertionMode=DatabaseAssertionMode.NON_STRICT,然后省略我不想要的列。