DbUnit - Warning: AbstractTableMetaData
Asked Answered
F

6

20

I am using DbUnit in the latest version 2.4.8 and I get many warnings in my Unit tests with this message:

WARN : org.dbunit.dataset.AbstractTableMetaData - 
Potential problem found: The configured data type factory 
    'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' 
     might cause problems with the current database 'MySQL' (e.g. some datatypes may 
     not be supported properly). In rare cases you might see this message because the 
     list of supported database products is incomplete (list=[derby]). If so please 
     request a java-class update via the forums.If you are using your own 
     IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override 
     getValidDbProducts() to specify the supported database products.

So I thought I add this (I use a MySQL database):

protected void setUpDatabaseConfig(DatabaseConfig config) {
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
}

But this does not help to avoid these warnings. What's wrong here?

Thank you in advance & Best Regards Tim.

Following answered 15/10, 2010 at 14:6 Comment(3)
Have you tried the "If so please request a java-class update via the forums" path?Heavenward
No, that is this one: MySqlDataTypeFactory. It should be okay.Following
Can you show your code? Where are you calling setUpDatabaseConfig?Oomph
M
34

I solved this with info from the dbunit faq. Just setting the data type factory property made the warning go away.

    Connection dbConn = template.getDataSource().getConnection();

    IDatabaseConnection connection = new DatabaseConnection(dbConn, "UTEST", false);

    DatabaseConfig dbConfig = connection.getConfig();

    // added this line to get rid of the warning
    dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new OracleDataTypeFactory());
Morentz answered 6/5, 2011 at 15:49 Comment(2)
Got it! For Mysql database, just replace the new OracleDataTypeFactory() in last line by new MySqlDataTypeFactory().Thimblerig
Tip: Look to all known implementing classes to find the one matching your database.Vainglorious
L
11

With Spring-Boot you can use such configuration bean

@Configuration
public class DbUnitConfiguration {

@Autowired
private DataSource dataSource;

@Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection() {
    DatabaseConfigBean bean = new DatabaseConfigBean();
    bean.setDatatypeFactory(new MySqlDataTypeFactory());

    DatabaseDataSourceConnectionFactoryBean dbConnectionFactory = new DatabaseDataSourceConnectionFactoryBean(dataSource);
    dbConnectionFactory.setDatabaseConfig(bean);
    return dbConnectionFactory;
    }
}
Lemberg answered 29/3, 2017 at 15:27 Comment(3)
DbUnitTestExecutionListener mentions COMMON_DATABASE_CONNECTION_BEAN_NAMES = { "dbUnitDatabaseConnection", "dataSource" }; so the name of the bean does matter!Phycomycete
You could instead pass the data source as a parameter for the method. This is useful if you're setting up the data source in the same configuration class: dataSourceInitializer(@Qualifier("dataSource") final DataSource dataSource)Hanshaw
This answer deserves more upvotes.Clausius
D
4

I know this is an old thread but all the answers here are more complicated than they need to be.

The simplest way to accomplish setting the factory on every connection acquisition is to supply an OperationListener and implement its connectionRetrieved method to do what you want. No overriding needed; the listener will be invoked every time an IDatabaseConnection is acquired.

Doublequick answered 10/2, 2014 at 19:20 Comment(1)
All links in this answer are dead.Burning
C
3

I was using JTDS driver and MS SQL 2008. In my DBUntiTest class override the following method. The waring message disappeared.

@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MsSqlDataTypeFactory());
}
Carisa answered 19/5, 2012 at 22:16 Comment(0)
P
1

@reassembler's answer is spot on. Just to add that I am testing against different database products, so I now set the DataType Factory according to the current connection:

private IDatabaseConnection getConnection(Connection jdbcConnection) throws Exception {
    String databaseProductName = jdbcConnection.getMetaData().getDatabaseProductName();

    DatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
    DatabaseConfig dbConfig = databaseConnection.getConfig();

    switch (databaseProductName) {
    case "HSQL Database Engine":
        dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
        break;
    case "MySQL":
        dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
        break;
    default:
        log.warn("No matching database product found when setting DBUnit DATATYPE_FACTORY");
    }

    return databaseConnection; 
}

You can obviously add any additionaly databases to this list.

Poetize answered 4/9, 2014 at 8:29 Comment(0)
S
0

I am using Dbunit version 2.7.0. In my case, just setting the data type factory property in the @Test doesn't suffice. The warning continues when calling dbunit JdbcDatabaseTester.onsetup() method.

I solved the problem implementing a MyJdbcDatabaseTester that extends JdbdDatabaseTester, and overriding the method getConnection(), configuring the datatype factory property:

public class MyJdbcDatabaseTester extends JdbcDatabaseTester {

public MyJdbcDatabaseTester(String driverClass, String connectionUrl, String username,
                            String password )
        throws ClassNotFoundException {
    super( driverClass, connectionUrl, username, password );
}

@Override
public IDatabaseConnection getConnection() throws Exception {
    IDatabaseConnection result = super.getConnection();
    DatabaseConfig dbConfig = result.getConfig();
    //to supress warnings when accesing to database
    dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
    return result;
}

}

Then I use MyJcbdDatabaseTester instead of JdbcDatabaseTester in my tests

Sizing answered 9/4, 2021 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.