spring-boot - 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 创建模式的设置。我也禁用了它。
有人可以帮我理解为什么脚本会运行两次吗?
解决方案
推荐阅读
- python - run_algorithm() 失败,因为它无法从 Internet 源获取数据(403 Forbidden)
- tinymce - 所见即所得编辑器不会在 Chrome 中激活
- r - 如何使用 ggplot2 用多边形列表绘制我的形状?
- javascript - 如何检查数组中的对象是否有空字符串?
- c - 如何修复由-Wconversion引起的错误?
- html - 如何修改我的 css 或 cshtml 以获得类似于 w3school 上的下拉导航栏?
- python - Python代码在程序/代码的开头跳回?
- firebase - 我将如何每周运行一次 javascript 代码?
- gtfs - 什么信息来源是交通 Web 应用程序的行业标准 - GTFS 与 Trapeze 数据文件
- python-3.x - 无法为 python 3 安装 openssl