我正在使用
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 – 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.
当我使用没有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中解决这个问题.请帮忙.
解决方法
问题解决了.
我将以下配置添加到applicationContext.xml(context.xml)中.
我将以下配置添加到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>