本文介绍了春季测试DBunit警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用spring-test-dbunit,并且在单元测试中收到以下警告:
I am using spring-test-dbunit and I get a warning in my Unit tests with this message:
代码:
@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());
}
}
警告:
当我使用不带spring-test-dbunit的DBunit时,可以解决以下问题:
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());
我不知道如何在spring-test-dbunit中解决此问题.请帮忙.
I don't know how to solve this problem in spring-test-dbunit. Please help.
推荐答案
问题已解决.我将以下配置添加到applicationContext.xml(context.xml)中.
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>
这篇关于春季测试DBunit警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!