1

I'm using DBUnit for testing my database. My database is not empty, So what I want is to ignore existing elements and to test just data inserted by my test.

This is an example of how test is run :

1- Table contains 10 elements

2- DBUnit insert some data from the dataset (3 elements)

3- My test insert data (1 element)

4- My expected dataset contains 4 elements which are the 3 elemnts defined in the first dataset and the element recently added by the test

5- So, when I do an assert equals of the actual and the expected table it shows me an error, wich is normal because my table already contains elements.

The question is : Is there any way to ignore elements existing in the database in the assert ? I want just to test data inserted by dataset and test.

This is the code :

@Override
    protected IDataSet getDataSet() throws Exception {
        // transforme fichier XML en BDD
        URL url = this.getClass().getResource("/dataset-peqt2-init.xml");
        File testFile = new File(url.getFile());
        return new FlatXmlDataSetBuilder().build(testFile);
    }



    @Override
    protected DatabaseOperation getSetUpOperation() throws Exception
    {
        return DatabaseOperation.REFRESH;
    }


    /**
     * Reset the state of database 
     * Called before every test
     */
    @Override
    protected DatabaseOperation getTearDownOperation() throws Exception
    {
        return DatabaseOperation.DELETE;
    }


    /**
     * get the actual table from the database
     * @param tableName
     * @return
     * @throws Exception
     * @throws SQLException
     * @throws DataSetException
     */
    private ITable getActualTable(String tableName) throws Exception, SQLException, DataSetException {
        // get the actual table values
        IDatabaseConnection connection = getConnection();
        IDataSet databaseDataSet = connection.createDataSet();
        return databaseDataSet.getTable(tableName);
    }

    /**
     * get the expected table from the dataset
     * @param tableName
     * @param fileName
     * @return
     * @throws Exception
     */
    private ITable getExpectedTable(String tableName, String fileName) throws Exception {
        // get the expected table values
        URL url = this.getClass().getResource("/"+fileName);
        File testFile = new File(url.getFile());
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(testFile);
        return expectedDataSet.getTable(tableName);
    }


    @Test 
    public void test01_insert() throws SQLException, Exception {
        File file = new File(SynchroDerbi.class.getResource("/test-insert.lst").getFile());
        log.debug("test01_insert() avec ref : "+file.getName());
        SynchroDerbi.run(file);
        String fileName = "dataset-insert-expected.xml";
        actualTable = getActualTable("equipment");
        expectedTable = getExpectedTable("equipment",fileName);

        Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, new String[]{"id","idSite"});

    }
Marouane
  • 21
  • 8

2 Answers2

1

Do DatabaseOperation.CLEAN_INSERT; in getSetUpOperation(). In this way the operation first will delete all table records and then will do your dataset insert.

0

Your tests shouldn't depend on the current state of the system. So instead of asserting for equality you should use "contains" checks. You get the results from select and then assert that those contain the results you just inserted.

If you want to be more strict with your checks (which may impact the maintainability of the tests) you can select the N of records BEFORE, then do inserts and then check if BEFORE+N = AFTER.

PS: DBUnit is not a very flexible and maintainable tool. Instead you could use the code of your system to save the state. That way if a column changes - you don't need to change DBUnit data. To ensure your tests don't step on each other toes use data randomization. Using your system's code may further help with isolation if you can start and rollback transactions within the tests.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45