Search code examples
dbunit

Loading null values with CsvDataFileLoader in dbunit


We are using the CsvDataFileLoader to load in our reference data like so:

new InsertIdentityOperation(DatabaseOperation.CLEAN_INSERT)
            .execute(connection,
                    new CsvDataFileLoader().load("/sql/ReferenceData/"));

Is there anyway to put null values into a csv that is loaded into our db.

I don't think there is, I would imagine that , null and NULL would all get interpreted as their string values.

Has anyone managed to do this or know of a work around for this problem?


Solution

  • CsvDataFileLoader uses CsvURLProducer to load and parse the data.

    In that class on Line 145 for dbunit 2.4.8 you see the following:

    if (CsvDataSetWriter.NULL.equals(row[col])) {
        row[col] = null;
    }
    

    CsvDataSetWriter.NULL contains the string "null" therefore your assumption that null would get interpreted as a string value appears to be incorrect and you should use this in your CSV.

    Of course this means that you can't have the string "null" in your fields but I'm sure this isn't often required.