[英]How to run embedded TomEE for integration tests with test resources
I have J2EE project based on maven. 我有基于Maven的J2EE项目。 This project contains connection to database, which is set up via resources.xml and persistence.xml .
这个项目包含连接到数据库,它是通过resources.xml中和的persistence.xml成立。 Connection works fine for normal deployment.
对于正常部署,连接工作正常。
My problem is, that i would like to run embedded TomEE server for integration tests. 我的问题是,我想运行嵌入式TomEE服务器进行集成测试。 For these tests i need use inmemory database.
对于这些测试,我需要使用内存数据库。
To start TomEE i use combination of maven plugins showed below. 要启动TomEE,请使用下面显示的Maven插件组合。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
</plugin>
When i start maven goal mvn install , server runs as expected, but with wrong DB connection. 当我开始Maven的目标MVN安装 ,服务器运行正常,但错误的数据库连接。 I did not find way, how to set up, that i need use src/test/resources instead of src/main/resources .
我没有找到办法,如何设置,我需要使用的src /测试/资源代替的src / main /资源 。
Do you know how to set up this plugin in way? 您知道如何以方式设置此插件吗? I'am open to suggestions of similar solutions, which is simple and do not contains 'lot of' frameworks.
我愿意接受类似解决方案的建议,这些解决方案很简单,并且不包含“很多”框架。
Thank you in advance. 先感谢您。
After some investigation i found solution described below. 经过一番调查,我找到了下面描述的解决方案。
Three plugins were added into pom. 三个插件被添加到pom中。
First plugin will start TomEE server for us. 第一个插件将为我们启动TomEE服务器。
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
Second one will replace proper resources and persistence files. 第二个将替换适当的资源和持久性文件。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<!--backup the "proper" files-->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml.proper"/>
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper"/>
<!--replace the "proper" files with the "test" version-->
<copy file="${project.build.testOutputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml"/>
<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" files -->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml.proper" tofile="${project.build.outputDirectory}/META-INF/resources.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>
Last plugin force 'clean' before install. 最后一个插件在安装前强制“清理”。 Without this i had problem in case, when i forget clean project before install.
没有这个,我遇到问题,以防万一我在安装之前忘记了干净的项目。
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.