Spring集成测试-从另一个项目生成数据库模式
Posted
技术标签:
【中文标题】Spring集成测试-从另一个项目生成数据库模式【英文标题】:Spring integration test- database schema generation from another project 【发布时间】: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 吗? 您是否尝试将"您是否尝试过在 persistence.xml 中指定 jar 文件?见https://***.com/a/1780362/204950
或者尝试将exclude-unlisted-classes设置为false,见https://***.com/a/1780437/204950
【讨论】:
将不包括未列出的类设置为 false 或 true 没有任何结果。将 jar 复制到目标中的依赖文件夹有效,但不适用于测试。唯一有帮助的是在persistence.xml中使用以上是关于Spring集成测试-从另一个项目生成数据库模式的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot Test 无法使用 JUnit 5 为自定义集成测试源集加载应用程序上下文
运行 Spring Boot 集成测试时出现 Liquibase 问题