在使用spring的jdbc:initialize-database标签进行单元测试时,发现这个标签与dataSource一同使用时,SQL脚本没有被正确执行,测试过程如下:
applicationContext-jdbc.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" default-lazy-init="true"> <!-- Jdbc单数据源事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven proxy-target-class="true" /> <context:property-placeholder ignore-resource-not-found="true" location="classpath*:/application.properties, classpath*:/application.test.properties" /> <!-- DBCP连接池--> <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.username}" /> <property name="password" value="${jdbc.password}" /> <property name="defaultAutoCommit" value="false" /> </bean> <jdbc:initialize-database data-source="dataSource" ignore-failures="ALL"> <jdbc:script location="classpath:sql/h2/schema.sql" /> <jdbc:script location="classpath:sql/h2/import-data.sql" encoding="UTF-8" /> </jdbc:initialize-database> <!-- <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:sql/h2/schema.sql" /> <jdbc:script location="classpath:sql/h2/import-data.sql" encoding="UTF-8" /> </jdbc:embedded-database> --> </beans>
?java测试代码为:
package com.shagri.showcase.data; import org.junit.Assert; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; @ContextConfiguration("classpath:jdbc/applicationContext-jdbc.xml") public class DataInitTest extends AbstractTransactionalJUnit4SpringContextTests { @Test public void testData(){ Assert.assertEquals(6,countRowsInTable("ss_user")); } }
? ? ?这里的import-data.sql和schema.sql是从springside4里面取出来的两个文件,在import-data.sql对ss_user表有6个insert语句。所以这里单元测试的期望是表中数据条数为6,但是遗憾的是,这里的测试失败,实际得到的数据条数为0。
? ? ?但是如果将xml文件中的dataSource和jdbc:initialize-database注释掉,而启用jdbc:embedded-database标记时,单元测试结果正确 。
? ? ?想与各位探讨一下这两种使用方式的不同之处,以及为什么会出现这样的问题。