Spring Boot H2单元测试设置两次运行导入脚本

时间:2018-12-07 00:45:27

标签: spring-boot h2

以下是我的DAO类的测试文件:

@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan(basePackages = {"com.foo.bu"})
@ContextConfiguration(
        locations = "/META-INF/test-config.xml")
@SpringBootTest(classes={MyDao.class})
@Transactional
public class MyDaoTest {

    @Autowired MyDao myDao;

    @Test
    public void getAllDataSet() {
        List<ProductEntity> list = myDao.getAllData();
        System.out.println(list.size());
    }
}

test-config.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: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/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

   <bean id="workflowhost" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan">
      <array>
              <value>com.foo.bu.dao</value>
              <value>com.foo.bu.entity</value>
         </array>
      </property>
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
      </property>
      <property name="jpaProperties">
         <props>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
         </props>
      </property>
   </bean>

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.h2.Driver" />
      <property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=TRUE;DB_CLOSE_DELAY=10;INIT=RUNSCRIPT FROM 'src/test/resources/data.sql'" />
      <property name="username" value="" />
      <property name="password" value="" />
   </bean>  

   <bean id="JpaTxnManager_workflowhost" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="workflowhost" />
   </bean>
   <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
   <tx:annotation-driven  transaction-manager="JpaTxnManager_workflowhost"/>

   <bean id="persistenceExceptionTranslationPostProcessor"
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

我的data.sql基本上包含sql脚本,该脚本创建了一堆表并将模拟记录插入到表中。

当我运行测试文件时,对于试图插入记录的sql脚本,我得到了与“唯一索引或主键冲突”相关的异常。发生这种情况的唯一方法是脚本执行两次。我不确定为什么会这样。通过添加子句CREATE TABLE IF NOT EXISTS

,我为CREATE TABLE脚本避免了这个问题

我浏览了文档https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html,该文档谈到如果我们不希望Hibernate创建架构,则设置hibernate.hbm2ddl.auto:none。我也已禁用它。

有人可以帮助我理解为什么脚本运行两次吗?

0 个答案:

没有答案