当前位置: 首页 > 知识库问答 >
问题:

如何使用测试资源为集成测试运行嵌入式TomEE

澹台俊晖
2023-03-14

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

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

提前谢谢你。

共有1个答案

吕皓
2023-03-14

经过一些调查,我发现解决方案如下所述。

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>
 类似资料:
  • 我有几个繁重的Spring集成测试(是的,这不是最好的方法,我没有时间正确地模拟所有外部dep) 下面是测试的典型注释 由于以下原因,测试会定期失败: 这里有两个问题:1、让测试共存的正确方式是什么?我在surefire插件中设置了forkCount=0。好像有帮助 2.1. 在每次测试期间,我实际上不需要启动所有的

  • 现在我们有一个项目,包含两个工作。1) 是带有单元测试的标准构建。2) 是集成测试。它们是这样工作的: 构建整个项目,运行单元测试,启动集成测试工作 问题是步骤2)现在需要一个多小时才能运行,我想将集成测试并行化,以便它们花费更少的时间。但我不确定我该怎么做。我的第一个想法是,我可以有两个这样的步骤: 构建整个项目,运行单元测试,启动集成测试工作 构建整个项目,将其部署到integration s

  • 问题内容: 当我使用命令:mvn test时,maven使用主要资源而不是src / test / resources中的测试资源。 我如何才能使Maven使用测试资源而不是主要资源? 编辑:我使用Classloader查找我的资源。Classloader可以从我的src / test / resources目录中找到资源,但是它首先在src / main / java中查找该资源。 还是在运行将

  • 我对我的spring boot应用程序进行了一些集成测试。spring boot根据依赖关系(和类路径JAR)选择要启动的服务器:(tomcat只有spring-boot-starter-web,如果有spring-boot-starter-undertow,则为undertow,如果有spring-boot-starter-undertow,则为jetty) 我正在编写一个过滤器,它应该可以在许

  • 集成测试是对已经进行单元测试的各个部分的一种整合测试。集成是昂贵的,并且它出现在测试中。你必须把这个考虑到你的预计和时间表里。 理想情况下,你应该这样组织一个项目,使得最后没有一个阶段是必须通过显式集成来进行的。这比在项目过程中,随着事情完成逐渐集成事情要好得多。如果这是不可避免的,请仔细评估。

  • 我目前正在开发一个解决方案,该解决方案目前有32个UnitTests。我一直在和resharper测试跑步者一起工作--它工作得很好。所有测试都在运行,所有测试都显示了正确的测试结果。 但是,在使用Visual Studio测试资源管理器时,测试不会运行。 Test Explorer将显示所有单元测试,但是一旦单击“Run all”,所有测试都将变灰,并且不显示测试运行的结果: 所有测试类都是公共