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

时间:2017-12-04 14:09:29

标签: java maven integration-testing

我有基于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

你知道如何设置这个插件吗?我对类似解决方案的建议持开放态度,这很简单,并且不包含“很多”框架。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

将三个插件添加到pom中。

第一个插件将为我们启动TomEE服务器。

CREATE TABLE `city` (
  `city_id` int(11) NOT NULL AUTO_INCREMENT,
  `city_name` varchar(45) NOT NULL,
  `province_id` int(11) NOT NULL,
  PRIMARY KEY (`city_id`,`city_name`),
  KEY `province_idx` (`province_id`),
  CONSTRAINT `province` FOREIGN KEY (`province_id`) REFERENCES `province` (`province_id`) 
) 

CREATE TABLE `province` (
  `province_id` int(11) NOT NULL AUTO_INCREMENT,
  `provice_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`province_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

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