弹簧测试 DB 单元警告

2022-09-03 09:44:57

我正在使用spring-test-dbunit,并且在我的单元测试中收到一条警告,其中包含以下消息:

法典:

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

}

警告:

1093 [main] WARN org.dbunit.dataset.AbstractTableMetaData - 发现潜在问题:配置的数据类型工厂'class org.dbunit.dataset.datatype.DefaultDataTypeFactory'可能会导致当前数据库'MySQL'出现问题(例如,某些数据类型可能未得到正确支持)。在极少数情况下,您可能会看到此消息,因为受支持的数据库产品列表不完整(list=[derby])。如果是这样,请通过论坛请求java类更新。如果您使用的是自己的 IDataTypeFactory 扩展 DefaultDataTypeFactory,请确保覆盖 getValidDbProducts() 以指定受支持的数据库产品。

当我使用DBunit而不使用spring-test-dbunit时,可以解决这个问题,如下所示:

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

我不知道如何在spring-test-dbunit中解决这个问题。请帮忙。


答案 1

问题解决了。我将以下配置添加到 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>

答案 2

只是想添加与 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;
}

推荐