我有一组基于分层架构定义的maven项目,如下所示:
1-域
-src/main/java
-src/main/resources/META-INF/
-src/test/java
-src/test/resources/META-INF/
2-存储库(取决于域)
-src/main/java
-src/main/resources/META-INF/
-src/test/java
-src/test/resources/spring/my-appcontext.xml
-src/test/resources/META-INF/orm.xml
我在存储库层中的单元测试将DbUnit与hsql内存数据库结合使用。持久性配置基于JPA / Hibernate,通过spring配置。下面是我在测试中使用的弹簧配置示例:
<!-- our datasource -->
<bean id="myDatasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${datasource.driverClassName}" />
<property name="url" value="${datasource.url}" />
<property name="username" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</bean>
<!-- let hibernate be in between the database and JPA -->
<bean id="myJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${jpa.database}" />
<property name="showSql" value="${jpa.showSql}" />
<property name="generateDdl" value="${jpa.generateDdl}" />
<property name="databasePlatform" value="${jpa.databasePlatform}" />
</bean>
<!-- entityManagerFactory - CONTAINER-managed -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="molecularanalysisDatasource"
p:mappingResources="${jpa.orm}"
p:packagesToScan="com.mypackages.domain">
<property name="jpaVendorAdapter" ref="myJpaVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.showSql">${hibernate.showSql}</prop>
<prop key="hibernate.formatSql">${hibernate.formatSql}</prop>
<prop key="dialect">${hibernate.dialect}</prop>
<prop key="hibernate.connection.driver_class">${datasource.driverClass}</prop>
<prop key="hibernate.connection.url">${datasource.url}</prop>
<prop key="hibernate.connection.username">${datasource.username}</prop>
<prop key="hibernate.connection.password">${datasource.password}</prop>
</props>
</property>
</bean>
<bean id="dbUnitDatabaseConfig"
class="com.github.springtestdbunit.bean.DatabaseConfigBean">
<property name="qualifiedTableNames" value="true" />
</bean>
<bean id="dbUnitDatabaseConnection"
class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
<property name="dataSource" ref="myDatasource" />
<property name="databaseConfig" ref="dbUnitDatabaseConfig" />
<property name="schema" value="${datasource.schema}" />
</bean>
我在测试范围中使用的orm.xml确保在hsqldb中使用默认目录名PUBLIC创建表。换句话说,在测试范围中使用orm.xml确保我的测试不关心在域实体上定义的特定目录/模式名称,而只是使用hsqldb下的默认名称(这是因为hsql只允许一个目录,即PUBLIC)在测试期间创建表时。
当我使用Eclipse运行测试时,可以找到“src / test / resources / META-INF / orm.xml”下的orm.xml,并由entitymanagerfactory用于在hsqldb中创建表。 但是,如果我使用maven运行测试,则不会选择orm.xml,因此实体管理器尝试使用真实的目录和模式名称创建我的表(实体),这将在hsqldb中失败,因为它只有一个目录,即PUBLIC。
在春季启用调试后,我注意到这是因为entitymanagerfactory在配置Jpa / Hibernate和持久性对象时使用不同的“根URL”来搜索配置文件。
如果通过Eclipse运行测试,实体管理器使用可以找到META-INF / orm.xml的“target / test-classes /”目录。在通过Maven运行测试的情况下,实体管理器使用domain.jar,其中不包括META-INF / orm.xml(因为这仅在集成模块中的测试中定义)。调试信息下方:
1-通过Eclipse运行测试:
[main] DEBUG - Processing PersistenceUnitInfo [
name: default
persistence provider classname: null
classloader: sun.misc.Launcher$AppClassLoader@35a16869
Temporary classloader:
org.springframework.instrument.classloading.SimpleThrowawayClassLoader@706f2039
excludeUnlistedClasses: true
JTA datasource: null
Transaction type: RESOURCE_LOCAL
PU root URL: file:/C:/DEVELOPMENT/project/integration/test-classes/
Shared Cache Mode: UNSPECIFIED
Validation Mode: AUTO
Jar files URLs []
2-通过Maven运行测试:
[main] DEBUG - Processing PersistenceUnitInfo [
name: default
persistence provider classname: null
classloader: sun.misc.Launcher$AppClassLoader@35a16869
Temporary classloader:
org.springframework.instrument.classloading.SimpleThrowawayClassLoader@706f2039
excludeUnlistedClasses: true
JTA datasource: null
Transaction type: RESOURCE_LOCAL
PU root URL: file:/C:/DEVELOPMENT/project/maven-repository/domain/domain.jar
Shared Cache Mode: UNSPECIFIED
Validation Mode: AUTO
Jar files URLs []
我无法工作的是如何在扫描orm.xml文件时使entitymanager包含META-INF / orm.xml。有谁知道在测试中如何在Maven中完成这项工作?将此orm.xml添加到域(src / main / resources / META-INF)将无济于事,因为此设置应仅用于测试上下文。