【问题标题】:How to run embedded TomEE for integration tests with test resources如何使用测试资源运行嵌入式 TomEE 进行集成测试
【发布时间】:2017-12-04 14:09:28
【问题描述】:

我有基于 maven 的 J2EE 项目。该项目包含通过resources.xml 和persistence.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 时,服务器按预期运行,但数据库连接错误。我没有找到方法,如何设置,我需要使用 src/test/resources 而不是 src/main/resources。

你知道如何设置这个插件吗?我愿意接受类似解决方案的建议,这些解决方案很简单,不包含“很多”框架。

提前谢谢你。

【问题讨论】:

    标签: java maven integration-testing


    【解决方案1】:

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

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

    安装前最后一个插件强制“清理”。如果没有这个,我会遇到问题,如果我在安装之前忘记了干净的项目。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多