如何配置JPA以在Maven中进行测试 [英] How to configure JPA for testing in Maven
问题描述
有没有办法在Maven项目中设置第二个persistence.xml文件,以便用于测试而不是用于部署的普通文件?
Is there a way to set up a second persistence.xml file in a Maven project such that it is used for testing instead of the normal one that is used for deployment?
我尝试将一个persistence.xml放入src / test / resources / META-INF,它被复制到target / test-classes / META-INF中,但它似乎是target / classes / META-INF(来自于src / main / resources)首选,尽管 mvn -X test
以正确的顺序列出类路径条目:
I tried putting a persistence.xml into src/test/resources/META-INF, which gets copied into target/test-classes/META-INF, but it seems target/classes/META-INF (the copy from the src/main/resources) gets preferred, despite mvn -X test
listing the classpath entries in the right order:
[DEBUG] Test Classpath :
[DEBUG] /home/uqpbecke/dev/NetBeansProjects/UserManager/target/test-classes
[DEBUG] /home/uqpbecke/dev/NetBeansProjects/UserManager/target/classes
[DEBUG] /home/uqpbecke/.m2/repository/junit/junit/4.5/junit-4.5.jar
...
我希望能够针对简单的hsqldb配置运行测试,而无需更改JPA配置的部署版本,项目结账后理想情况下是直接的不需要进行本地调整。
I would like to be able to run tests against a simple hsqldb configuration without having to change the deployment version of the JPA configuration, ideally straight after project checkout without any need for local tweaking.
推荐答案
以下内容适用于Maven 2.1+(之前没有阶段)可以将执行绑定到测试和包之间。)
The following will work for Maven 2.1+ (prior to that there wasn't a phase between test and package that you could bind an execution to).
您可以使用maven-antrun-plugin将persistence.xml替换为持续时间的测试版本测试,然后在打包项目之前恢复正确的版本。
You can use the maven-antrun-plugin to replace the persistence.xml with the test version for the duration of the tests, then restore the proper version before the project is packaged.
此示例假设生产版本为src / main / resources / META-INF / persistence.xml测试版本是src / test / resources / META-INF / persistence.xml,因此它们将分别复制到target / classes / META-INF和target / test-classes / META-INF。
This example assumes the production version is src/main/resources/META-INF/persistence.xml and the test version is src/test/resources/META-INF/persistence.xml, so they will be copied to target/classes/META-INF and target/test-classes/META-INF respectively.
将它封装成一个mojo会更优雅,但因为你只是复制一个文件,所以看起来有点过分。
It would be more elegant to encapsulate this into a mojo, but as you're only copying a file, it seems like overkill.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<!--backup the "proper" persistence.xml-->
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper"/>
<!--replace the "proper" persistence.xml with the "test" version-->
<copy file="${project.build.testOutputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>restore-persistence</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<!--restore the "proper" persistence.xml-->
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
这篇关于如何配置JPA以在Maven中进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!