如何 运行 嵌入 TomEE 以使用测试资源进行集成测试

How to run embedded TomEE for integration tests with test resources

我有基于maven 的J2EE 项目。该项目包含与数据库的连接,通过 resources.xmlpersistence.xml 设置。正常部署的连接工作正常。

我的问题是,我想 运行 嵌入式 TomEE 服务器进行集成测试。对于这些测试,我需要使用内存数据库。

为了启动 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>

当我启动 maven 目标 mvn install 时,服务器 运行s 按预期运行,但数据库连接错误。我没有找到方法,如何设置,我需要使用 src/test/resources 而不是 src/main/resources.

你知道如何设置这个插件吗?我乐于接受类似解决方案的建议,这些解决方案很简单并且不包含 'lot of' 框架。

提前致谢。

经过一些调查,我找到了下面描述的解决方案。

pom 添加了三个插件。

第一个插件将为我们启动 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>

第二个将替换适当的资源和持久性文件。

<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>

最后一次插件强制 'clean' 安装前。如果没有这个,我会遇到问题,因为我在安装前忘记清理项目。

<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>