Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Test DBunit Warning

I am using spring-test-dbunit and I get a warning in my Unit tests with this message:

Code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/context.xml"})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class })
public class TestDB {

    @Autowired 
    private ICourseService courseService;

    @Test
    @DatabaseSetup("sampleData.xml")
    public void testFind() throws Exception {
        List<Course> courseList = this.courseService.getAllCourses();

        assertEquals(1, courseList.size());
        assertEquals("A001", courseList.get(0).getCourseNumber());
    }

}

Warning:

1093 [main] 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.

The problem can be solved when I use DBunit without spring-test-dbunit as follow:

  Connection jdbcConnection = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", "root");
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());

I don't know how to solve this problem in spring-test-dbunit. Please help.

like image 214
Ye Kyaw Kyaw Htoo Avatar asked Dec 26 '14 03:12

Ye Kyaw Kyaw Htoo


3 Answers

Problem solved. I add the following configuration to applicationContext.xml (context.xml) .

<property name="location">
        <value>classpath:jdbc.properties</value>
    </property> 
</bean>  

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driver}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
</bean>

<bean id="sqlDataTypeFactory" class ="org.dbunit.ext.mysql.MySqlDataTypeFactory" />

<bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
     <property name = "datatypeFactory" ref = "sqlDataTypeFactory" />
</bean> 
<bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
    <property name="databaseConfig" ref="dbUnitDatabaseConfig"/>
    <property name="dataSource" ref="dataSource" />
</bean>
like image 145
Ye Kyaw Kyaw Htoo Avatar answered Oct 16 '22 01:10

Ye Kyaw Kyaw Htoo


Just wanted to add the same solution as Java Config:

@Bean
public DataSource dataSource() {
    DataSource dataSource = ...
    return dataSource;
}

@Bean
public DatabaseConfigBean dbUnitDatabaseConfig() {
    DatabaseConfigBean dbConfig = new com.github.springtestdbunit.bean.DatabaseConfigBean();
    dbConfig.setDatatypeFactory(new org.dbunit.ext.h2.H2DataTypeFactory());
    return dbConfig;
}

@Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection() {
    DatabaseDataSourceConnectionFactoryBean dbConnection = new com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean(dataSource());
    dbConnection.setDatabaseConfig(dbUnitDatabaseConfig());
    return dbConnection;
}
like image 11
Yser Avatar answered Oct 16 '22 03:10

Yser


To complement the existing answers, I just wanted to add what worked for me in a Spring Boot context, using the Spring Boot configured datasource. Add the following class in your test sources (in a package that will be picked up by autoconfig) :

@Configuration
public class DBUnitConfig {

    @Autowired
    private DataSource dataSource;

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

        DatabaseDataSourceConnectionFactoryBean dbConnectionFactory = new com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean(dataSource);
        dbConnectionFactory.setDatabaseConfig(bean);
        return dbConnectionFactory;
    }
}
like image 6
Pierre Henry Avatar answered Oct 16 '22 03:10

Pierre Henry