【发布时间】:2013-08-12 08:11:58
【问题描述】:
这是我使用 Spring 和嵌入式数据库 H2 进行集成测试的设置
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<jdbc:embedded-database id="dataSource" type="H2" />
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:sql/globalParams.sql"/>
<jdbc:script location="classpath:sql/customersGroupView.sql"/>
<jdbc:script location="classpath:sql/recentIntegrationsTableAndTrigger.sql"/>
<jdbc:script location="classpath:sql/insertIntegrationDate.sql"/>
<jdbc:script location="classpath:sql/toCharRoutine.sql"/>
</jdbc:initialize-database>
</beans>
集成测试的抽象父类
@ContextConfiguration(locations = [
"classpath:com/dhl/dcc/dcc-core.xml",
"classpath:com/dhl/dcc/test-security.xml",
"classpath:com/dhl/dcc/dcc-audit.xml",
"classpath:com/dhl/dcc/test-dataSource.xml",
"classpath:com/dhl/dcc/test-beans.xml",
"classpath:com/dhl/dcc/dcc-forms.xml"
])
public abstract class AbstractIntegrationTestCase extends AbstractTransactionalJUnit4SpringContextTests {
在实体管理器工厂的核心配置中
<property name="generateDdl" value="${dcc.orm.generateDdl:false}"/>
property dcc.orm.generateDdl 在属性中设置为 true。
它运行良好(数据库模式是从注释@Entity 的类生成的)但现在我将域模型分离到它自己的项目中,并将这个项目作为依赖项添加到 Maven 中。之后,由于缺少数据库架构,我的集成测试开始失败。我如何配置嵌入式数据库应该在哪里寻找域模型?谢谢。
编辑:实体工厂配置
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="DCC"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="${dcc.orm.generateDdl:false}"/>
<property name="showSql" value="${dcc.orm.showSql:false}"/>
<property name="databasePlatform" value="${dcc.orm.dialect}"/>
</bean>
</property>
</bean>
【问题讨论】:
-
您的 jar 是否包含此“classpath:sql/globalParams.sql”?通常测试资源不会打包进jar,而是*-test.jar。因此,如果 sql 文件在 test 目录下,请打包您的测试 jar 并将其添加到您的目标项目中。
-
脚本没有问题。他们正在被处决。问题是嵌入式数据库中的 DB 模式不是从 JPA 实体创建的。
-
抱歉造成误会。你能发布 spring xml 包含你的 entityManagerFactory 吗?
-
您是否尝试将"
" 添加到您的 entityManagerFactory?
标签: integration-testing embedded-database spring-test