简体   繁体   English

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

[英]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.

相关问题 Maven - 如何告诉 Maven 在 src/test/resources 下使用 application-integration.properties 运行集成测试? - Maven - How to tell Maven to run integration tests with application-integration.properties under src/test/resources? 如何运行集成测试? - How to run integration tests? 如何:针对测试环境(数据库)运行maven集成测试 - How to: Run maven integration tests against a test environment (database) 在mvn集成测试中没有运行硒测试 - No selenium tests run during mvn integration test 集成测试-嵌入式码头-如何设置数据 - integration tests - embedded jetty - how to setup data TomEE嵌入式配置,用于使用HSQLDB设置进行测试 - TomEE embedded configuration for test with HSQLDB setup JUnit和集成测试 - 是否可以在运行的任何测试之前运行一个 - JUnit & Integration tests - Is it possible to run one ahead of any test that is run Spring Boot集成测试-如何在单个上下文中运行多个测试类? - Spring Boot Integration Tests - How to run multiple test classes with a single context? 如何在集成测试中测试 Mongo 索引? - How to test Mongo indexes in integration tests? 如何为成熟的Android应用程序运行集成测试? - How to run integration tests for a mavenised Android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM