2014-12-26 13 views
11

Ich bin mit Feder-test-DBUnit und ich bekomme eine Warnung in meinen Unit-Tests mit dieser Nachricht:Frühlings-Test DBUnit Warnung

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

} 

Warnung:

1093 [main] WARN org.dbunit.dataset.AbstractTableMetaData - Potential Problem gefunden: Der konfigurierte Datentyp factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' könnteverursachenProbleme mit der aktuellen Datenbank 'MySQL' (z. einige Datentypen können nicht richtig unterstützt werden). In seltenen Fällen wird möglicherweise die folgende Nachricht angezeigt: , da die Liste der unterstützten Datenbankprodukte nicht vollständig ist (list = [derby]). Wenn ja, fordern Sie bitte ein Update der Java-Klasse über die Foren an. Wenn Sie Ihre eigene IDataTypeFactory verwenden, die DefaultDataTypeFactory erweitert, stellen Sie sicher, dass Sie getValidDbProducts() überschreiben, um die unterstützten Datenbankprodukte anzugeben.

Das Problem kann gelöst werden, wenn ich DBUnit ohne Feder Test-DBUnit wie folgt verwendet werden:

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

Ich weiß nicht, wie dieses Problem im Frühjahr-Test-DBUnit zu lösen. Bitte helfen Sie.

Antwort

15

Problem gelöst. Ich füge die folgende Konfiguration zu applicationContext.xml (context.xml) hinzu.

<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> 
10

Ich wollte nur die gleiche Lösung wie Java Config hinzuzufügen:

@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; 
} 
+0

Und stellen Sie sicher, dass alle Bean Namen heißt Methodennamen bleiben genau die gleiche wie @Yser oben erwähnt ... – Nick

1

Dank Lynn Niño. Die Antwort hat mir geholfen, die Konfiguration für meine H2-Datenbank zu beheben:

<?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> 
3

die vorhandenen Antworten Zur Ergänzung, wollte ich nur noch hinzufügen, was für mich gearbeitet in einem Frühlings-Boot-Kontext, das Frühling-Boot konfiguriert Datenquelle verwendet wird. Fügen Sie die folgende Klasse in Ihrem Testquellen (in einem Paket, das von autoconfig abgeholt werden):

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