spring-test-dbunit的配置和使用

琴镜
2023-12-01

1、数据源配置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="500" />
		<property name="defaultAutoCommit" value="true" />
		<property name="timeBetweenEvictionRunsMillis" value="10000" />
		<property name="minEvictableIdleTimeMillis" value="10000" />
		<property name="validationQuery" value="select 1 from dual" />
		<property name="testOnBorrow" value="true" />
		<property name="testOnReturn" value="true" />
		<property name="testWhileIdle" value="true" />
	</bean>

2、自定义数据库链接

<bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
		<property name="skipOracleRecyclebinTables" value="true" />
	</bean>

	<bean id="dbUnitDatabaseConnection"
		class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
		<property name="databaseConfig" ref="dbUnitDatabaseConfig" />
		<property name="dataSource" ref="dataSource" />
		<!-- oracle必须配置schema,不配置会抛AmbiguousTableNameException,mysql不要配置schema,否则执行@ExpectedDatabase的逻辑时会抛错 -->
		<property name="schema" value="TEST" />
	</bean>
注:如果数据库是mysql,可以忽略第二步,不需要配置自定义数据库链接。但如果使用的oracle,就需要配置,主要是为了指定数据库的schema,如果不配置的话,会抛出异常,如下:
org.dbunit.database.AmbiguousTableNameException: AA
	at org.dbunit.dataset.OrderedTableNameMap.add(OrderedTableNameMap.java:198)
	at org.dbunit.database.DatabaseDataSet.initialize(DatabaseDataSet.java:231)
	at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:281)
	at org.dbunit.operation.AbstractOperation.getOperationMetaData(AbstractOperation.java:80)
	at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:143)
	at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:146)
	at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:70)
	at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:158)
	at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

3、编写测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/spring/dbunit.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class })
@DbUnitConfiguration(databaseConnection = "dbUnitDatabaseConnection")
public class DaoTest {

    @Test
    @DatabaseSetup(type = DatabaseOperation.TRUNCATE_TABLE, value = { "expect.xml" })
    @ExpectedDatabase(value = "expect.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    @DatabaseTearDown(type = DatabaseOperation.DELETE, value = { "expect.xml" })
    public void testInsert() {
    }

    @Test
    @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = { "setup.xml" })
    @ExpectedDatabase(value = "setup.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    @DatabaseTearDown(type = DatabaseOperation.DELETE, value = { "expect.xml" })
    public void testQuery() {
    }

    @Test
    @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = { "setup.xml" })
    @ExpectedDatabase(value = "expect.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    @DatabaseTearDown(type = DatabaseOperation.DELETE, value = { "expect.xml" })
    public void testUpdate() {
    }

    
    @Test
    @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = { "setup.xml" })
    @ExpectedDatabase(value = "expect.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    public void testDelete() {
    }

}

注:

@ContextConfiguration(locations = {"classpath:conf/spring/dbunit.xml" })

加载spring配置文件,本例中的数据源和自定义数据库链接都是放在 conf/spring/dbunit.xml 这个配置文件中。

@DbUnitConfiguration(databaseConnection ="dbUnitDatabaseConnection")

如果使用了自定义数据库链接,则需要指定数据库链接的bean id,即 databaseConnection =  "dbUnitDatabaseConnection"

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,

        DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,

        DbUnitTestExecutionListener.class })

添加这个注解后,就可以在测试方法上使用@DatabaseSetup,@ExpectedDatabase,@DatabaseTearDown




参考资料:http://springtestdbunit.github.io/spring-test-dbunit/



转载于:https://www.cnblogs.com/puroc/p/3861348.html

 类似资料: