我正在使用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());
}
}
警告:
当我使用不带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)中。
<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>
关于java - Spring 测试DBunit警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27652689/