DbUnit - Warning: AbstractTableMetaData

2022-09-02 11:33:40

我在最新版本 2.4.8 中使用 DbUnit,并且在我的单元测试中收到许多警告,并显示以下消息:

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.

所以我想我添加这个(我使用MySQL数据库):

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

但这无助于避免这些警告。这是怎么回事?

提前感谢您和最好的问候蒂姆。


答案 1

我用dbunit FAQ中的信息解决了这个问题。只是设置数据类型 factory 属性就使警告消失。

    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());

答案 2

使用Spring-Boot,您可以使用这样的配置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;
    }
}

推荐