DAO的保存方法中的单元测试

时间:2017-10-11 07:27:02

标签: java spring unit-testing dbunit spring-test-dbunit

我写了一个单元测试来测试DAO的save方法,我确定我的save方法有效,但是,当我运行单元测试时遇到了这个错误:

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”fireld将无法匹配xml文件中的预期结果,如何忽略比较列“id”? 现在错误信息是:

junit.framework.ComparisonFailure: value (table=RECONCILIATION_OBJECT_HANDLER, row=3, col=id) expected:<[3]> but was:<[12]>

1 个答案:

答案 0 :(得分:0)

我通过阅读文档找到了解决方案: Spring Test DBUnit

我可以在注释@ExpectedDatabase中选择assertionMode = DatabaseAssertionMode.NON_STRICT,然后省略我不想要的列。