简体   繁体   中英

unit test in DAO's save method

I wrote a unit test to test the save method of DAO, I'm sure my save method works, however, when I running my unit test it ran into this error:

junit.framework.ComparisonFailure: row count (table=RECONCILIATION_OBJECT_HANDLER) expected:<[4]> but was:<[3]>

here's my unit test code:

@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);
}

here's my expected results:

<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" />

-----------------update---------------------------------------------

I realize I don't need to add rollback annotation, and I need to offer all the column information for result comparison, however, everytime I run unit test the INSERT operation will make the auto increment of column "id" resulting the "id" fireld will not matching the expected results in the xml files, how can I ignore compare column "id"? now the error message is:

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

I find the solution by reading the documentation: Spring Test DBUnit

I can choose assertionMode=DatabaseAssertionMode.NON_STRICT in the annotation @ExpectedDatabase and then omit columns that I don't want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM