首页 > 解决方案 > 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 脚本规避了这个问题CREATE TABLE IF NOT EXISTS

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

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

标签: spring-booth2

解决方案


推荐阅读