春季测试 DBunit 警告

Posted

技术标签:

【中文标题】春季测试 DBunit 警告【英文标题】:Spring Test DBunit Warning 【发布时间】:2015-02-23 12:05:22 【问题描述】:

我正在使用 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”的问题(例如,某些数据类型可能 没有得到适当的支持)。在极少数情况下,您可能会看到此消息 因为支持的数据库产品列表不完整 (列表=[德比])。如果是这样,请通过 论坛。如果您使用自己的 IDataTypeFactory 扩展 DefaultDataTypeFactory,确保覆盖 getValidDbProducts() 指定支持的数据库产品。

当我使用没有spring-test-dbunit的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;

【讨论】:

并确保所有 bean 名称,即方法名称与上面提到的 @Yser 完全相同...【参考方案3】:

为了补充现有的答案,我只想使用 Spring Boot 配置的数据源在 Spring Boot 上下文中添加对我有用的内容。在您的测试源中添加以下类(在将由 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;
    

【讨论】:

【参考方案4】:

感谢林恩尼诺。答案帮助我更正了我的 H2 数据库的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="isi.power.share" />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- H2 database JDBC settings -->    
    <bean id="dataSource"
          class="org.h2.jdbcx.JdbcDataSource">
        <property name="URL" value="jdbc:h2:mem:paging;DB_CLOSE_DELAY=-1;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS my_extra_schema;"/>
        <property name="user" value="root"/>
        <property name="password" value="password"/>
    </bean>    

    <!-- set the data type factory for dbunit -->
    <bean id="h2DataTypeFactory" class ="org.dbunit.ext.h2.H2DataTypeFactory" />

    <bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean"> 
         <property name = "datatypeFactory" ref = "h2DataTypeFactory" /> 
    </bean>  

    <bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean"> 
        <property name="databaseConfig" ref="dbUnitDatabaseConfig"/> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- provide a H2 console to look into the db if necessary -->
    <bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
        factory-method="createWebServer" depends-on="dataSource"
        init-method="start" lazy-init="false">
        <constructor-arg value="-web,-webAllowOthers,-webPort,8085" />
    </bean>

    <!-- provide a TCP server to look into the db if necessary -->
    <bean id="org.h2.tools.Server-TcpServer" class="org.h2.tools.Server"
        factory-method="createTcpServer" depends-on="dataSource"
        init-method="start" lazy-init="false">
        <constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9095" />
    </bean>

    <!-- define database entity manager factory & transaction manager -->

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="persistenceXmlLocation"  value="classpath:META-INF/SpringDatabaseTestPersistence.xml" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />     
    </bean>


</beans>

【讨论】:

以上是关于春季测试 DBunit 警告的主要内容,如果未能解决你的问题,请参考以下文章

DBUnit数据库测试

DBUnit数据库测试

DBUnit数据库测试

依赖数据库的单元测试——DBUnit

java 使用测试数据集进行DBUnit测试

关于unitils联合dbunit的测试