【发布时间】:2016-04-25 14:37:47
【问题描述】:
我正在开发一个 Java Spring+Hibernate 项目,我们有一个 Junit 设置来对我们的代码进行单元测试。 我现在面临的问题是我不明白为什么 Hibernate(使用 Spring-boot)在测试实际运行之前创建数据库模式 2 次。顺序如下:
- Alter 表删除所有外键
- 删除表(如果存在)
- 创建表
- 更改表添加约束(如 FK)
- Alter 表删除所有外键
- 删除表(如果存在)
- 创建表
- 更改表添加约束
- 执行所有测试
我的问题是,如果更具体一点:为什么要执行第 3-6 点? 为什么干脆不执行1,2,7,8,9。我为什么要这个?因为这需要宝贵的时间,我不明白我为什么需要这个。
下面是我的持久化配置:
<persistence-unit name="localContainerEntityForTest">
<description>Spring JPA LocalContainerEntityManagerFactoryBean</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.implicit_naming_strategy" value="legacy-jpa"/>
<property name = "hibernate.show_sql" value = "true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/myApp?createDatabaseIfNotExist=true"/>
<property name="javax.persistence.jdbc.user" value="hibernate"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
另外,下面是我用于每个单元测试类的注释:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = JPAConfigurationTestEnviorement.class)
@WebAppConfiguration
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
【问题讨论】:
标签: java mysql spring hibernate junit