lundi 23 septembre 2019

DBUnit 2.6.0 : NoSuchColumnException

I'm using DBUnit 2.6.0 with PostgreSQL 10 to populate my tables with the setUp method for my tests.

When I first tried it out, I got a NoSuchColumnException, despite my dataset seems correct : Here is the table structure I'm trying to put data in :

CREATE TABLE "user_profil" (
    "nni" VARCHAR(8) NOT NULL,
    "profile" VARCHAR(12) NOT NULL,
    "perimetre" VARCHAR(50) NOT NULL,
    "creadate" TIMESTAMP NULL,
    "modifdate" TIMESTAMP NULL,
    PRIMARY KEY ("nni", "profil", "perimetre")
)

Here is my flat XML file :

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <user_profil nni="TEST001" profile="support" perimetre="info1"/>
    <user_profil nni="TEST002" profile="admin" perimetre="info1" />
</dataset>

My Test class :

public class ThisIsATest extends TestConfig {

    protected static IDatabaseTester dbTester;

    @BeforeAll
    public static void setUpBeforeClass(TestInfo testInfo) throws Exception {
        dbTester = createDatabaseTester();
    }

    @BeforeEach
    protected void setUpBeforeEach(TestInfo testInfo) throws Exception {
            IDataSet dataSet = new FlatXmlDataSetBuilder().build(DatabaseLoadingUtil.getTestXmlFile());
            dbTester.setDataSet(dataSet);
            dbTester.setSetUpOperation(DatabaseOperation.REFRESH);
            dbTester.onSetup();
    }

    @AfterEach
    protected void tearDownAfterEach(TestInfo testInfo) throws Exception {
        dbTester.setTearDownOperation(DatabaseOperation.DELETE);
        dbTester.onTearDown();
    }

    private static IDatabaseTester createDatabaseTester() {
        IDatabaseTester tester = null;

        tester = new DataSourceDatabaseTester(getDataSource(), getJDBCUser()) 
        {
            @Override
            public IDatabaseConnection getConnection() throws Exception
            {
                IDatabaseConnection connection = super.getConnection();
                connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
                return connection;
            }
        };

        return tester;
    }

    private static DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(getJDBCDriverClassName());
        dataSource.setUrl(getJDBCURL());
        dataSource.setUsername(getJDBCUser());
        dataSource.setPassword(getJDBCPassword());

        return dataSource;
    }


    @Test
    public void testSomething {
        // test
    }
}

And here is the error I get :

org.dbunit.dataset.NoSuchColumnException: user_profil.NNI -  (Non-uppercase input column: nni) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.

When I runned in debug mode, I realized that DBUnit didn't succeed in getting the columns of the tables in the DB, so when it checks if the columns of the dataset are present in the table in the database, it fails. I saw that I had to set the property for PostgreSQL to avoid it, but as you can see I did (connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());), but it still doesn't work.

I tried to generate a DTD representing my database schema (to include it in the flatXml file, because it's highly recommendated by the DBUnit documentation), using this :

FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream("test.dtd"));

It generated me a "correct" DTD of my database, but only with the tables names, without any column :

<!ELEMENT dataset ( 
    application*,
    user*, 
    user_profil*)>

<!ELEMENT application EMPTY>
<!ATTLIST application
>

<!ELEMENT user EMPTY>
<!ATTLIST user
>

<!ELEMENT user_profil EMPTY>
<!ATTLIST user_profil
>

My last thought is that it could come from the connection to the database itself, but it's the same JDBC properties used by another program to access the DB, and it works fine ..

I try to figure out a solution but I'm a bit desperate right now ...

Someone have an idea?

Aucun commentaire:

Enregistrer un commentaire