mardi 3 mai 2016

How to ignore existing elements of tables when testing database with DBUnit

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

    }

Aucun commentaire:

Enregistrer un commentaire