7个月前 (09-25)  Java系列 |   抢沙发  175 
文章评分 0 次,平均分 0.0

一般情况下在Maven构建中添加集成测试有点麻烦,这是因为标准目录布局只有一个测试目录(src/test)。

如果我们想使用标准目录布局并将集成测试添加到Maven构建中,我们有两个选项:

首先,我们可以将集成测试添加到与单元测试相同的目录中。这是一个糟糕的想法,因为集成测试单元测试是完全不同的概念,这种方法迫使我们将它们混合在一起。此外,如果我们遵循这种方法,从IDE运行单元测试将成为一件麻烦事。当我们运行测试时,IDE会运行从测试目录中找到的所有测试。这意味着单元测试和集成测试都在运行。如果我们“幸运”的话,这意味着我们的测试套件比它可能的要慢,但这通常意味着集成测试每次都会失败。

其次,我们可以将集成测试添加到一个新模块中。这太过分了,因为它迫使我们将项目转换为多模块项目,只是因为我们想将集成测试与单元测试分开。此外,如果我们的项目已经是一个多模块项目,并且我们想为多个模块编写集成测试,那么我们就完蛋了。当然,我们总是可以为每个被测试的模块创建一个单独的集成测试模块,但这会减少我们自己的痛苦。

很明显,这两种解决方案都很糟糕。这篇文章描述了我们如何解决这些解决方案的问题。

我们的Maven构建的要求是:

  • 集成和单元测试必须有单独的源目录。src/integration-test/java目录必须包含集成测试的源代码,src/test/java目录则必须包含单元测试的源码。
  • 集成和单元测试必须具有不同的资源目录。src/integration-test/resources目录必须包含集成测试的资源,src/test/resources路径必须包含单元测试的资源。
  • 默认情况下只运行单元测试。
  • 必须可以只运行集成测试。
  • 如果集成测试失败,那么它一定会使我们的构建失败。
  • 每个集成测试类的名称必须以前缀“IT”开头。

让我们从为单元测试和集成测试创建Maven profiles文件开始。

为单元和集成测试创建Maven Profiles配置文件

首先,我们需要创建两个Maven profiles文件,如下所述:

dev profiles文件在开发环境中使用,它是Maven构建的默认profiles文件(即,当未指定profiles文件时,它是active的)。这个Maven profiles文件有两个目标,如下所述:

1. 它配置包含属性文件的目录的名称,该文件包含开发环境中使用的配置。

2. 它确保在开发profiles文件处于活动状态时只运行单元测试。

集成测试配置文件用于运行集成测试。集成测试profiles文件有两个目标,如下所述:

1. 它配置包含属性文件的目录的名称,该文件包含集成测试使用的配置。

2. 它确保只有在集成测试配置文件处于活动状态时才运行集成测试。

我们可以通过在pom.xml文件中添加以下XML来创建这些profiles文件:

<profiles>
    <!-- The Configuration of the development profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!--
                Specifies the build.profile.id property that must be equal than the name of
                the directory that contains the profile specific configuration file.
                Because the name of the directory that contains the configuration file of the
                development profile is dev, we must set the value of the build.profile.id
                property to dev.
            -->
            <build.profile.id>dev</build.profile.id>
            <!--
                Only unit tests are run when the development profile is active
            -->
            <skip.integration.tests>true</skip.integration.tests>
            <skip.unit.tests>false</skip.unit.tests>
        </properties>
    </profile>
    <!-- The Configuration of the integration-test profile -->
    <profile>
        <id>integration-test</id>
        <properties>
            <!--
                Specifies the build.profile.id property that must be equal than the name of
                the directory that contains the profile specific configuration file.
                Because the name of the directory that contains the configuration file of the
                integration-test profile is integration-test, we must set the value of the
                build.profile.id property to integration-test.
            -->
            <build.profile.id>integration-test</build.profile.id>
            <!--
                Only integration tests are run when the integration-test profile is active
            -->
            <skip.integration.tests>false</skip.integration.tests>
            <skip.unit.tests>true</skip.unit.tests>
        </properties>
    </profile>
</profiles>

其次,我们必须确保我们的应用程序使用正确的配置。当我们使用dev profiles文件运行它时,我们希望它使用开发环境中使用的配置。另一方面,当我们运行集成测试时,我们希望测试的代码使用为集成测试量身定制的配置。

我们可以通过以下步骤确保我们的应用程序使用特定于配置文件的配置:

1. 配置我们的Maven构建,从活动Maven配置文件的配置目录加载配置文件特定的配置文件(config.properties)。

2. 配置我们构建的资源目录(src/main/resources)并启用资源筛选。这确保了从我们的资源中找到的占位符被从配置文件特定的配置文件中读取的实际属性值所取代。

POM文件的相关部分如下:

<filters>
    <!--
        Ensures that the config.properties file is always loaded from the
        configuration directory of the active Maven profile.
    -->
    <filter>profiles/${build.profile.id}/config.properties</filter>
</filters>
<resources>
    <!--
        Placeholders that are found from the files located in the configured resource
        directories are replaced with the property values found from the profile
        specific configuration file.
    -->
    <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
    </resource>
</resources>

使用Maven创建特定于配置文件的配置文件:https://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/

让我们继续了解如何在Maven构建中添加额外的源代码和资源目录。

将额外的源和资源目录添加到我们的Maven构建中

Maven构建的要求是,我们的集成和单元测试必须有单独的源目录和资源目录。因为我们的单元测试的源代码和资源是从标准测试目录中找到的,所以我们必须为集成测试创建新的源和资源目录。

我们可以使用BuildHelper Maven插件(http://www.mojohaus.org/build-helper-maven-plugin/)将额外的源目录和资源目录添加到我们的构建中。我们通过以下步骤来满足我们的要求:

1. 将BuildHelper Maven插件添加到POM文件的插件部分。

2. 配置将新的源目录和资源目录添加到我们的构建中的执行。

首先,我们可以通过将以下XML添加到pom.xml文件的plugins部分,将BuildHelper Maven插件添加到我们的Maven构建中:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <!-- Add executions here -->
    </executions>
</plugin>

其次,我们必须配置将新的源目录和资源目录添加到Maven构建中的执行。我们可以通过以下步骤来做到这一点:

1. 创建一个执行,将新的源目录(src/integration-test/java)添加到我们的构建中。

2. 创建一个执行,将新的资源目录(src/integration-test/resources)添加到我们的构建中,并启用资源筛选。

我们可以通过将以下XML添加到Build Helper Maven插件的配置中来创建这些执行:

<!-- Add a new source directory to our build -->
<execution>
    <id>add-integration-test-sources</id>
    <phase>generate-test-sources</phase>
    <goals>
        <goal>add-test-source</goal>
    </goals>
    <configuration>
        <!-- Configures the source directory of our integration tests -->
        <sources>
            <source>src/integration-test/java</source>
        </sources>
    </configuration>
</execution>
<!-- Add a new resource directory to our build -->
<execution>
    <id>add-integration-test-resources</id>
    <phase>generate-test-resources</phase>
    <goals>
        <goal>add-test-resource</goal>
    </goals>
    <configuration>
        <!-- Configures the resource directory of our integration tests -->
        <resources>
            <!--
                Placeholders that are found from the files located in the configured resource
                directories are replaced with the property values found from the profile
                specific configuration file.
            -->
            <resource>
                <filtering>true</filtering>
                <directory>src/integration-test/resources</directory>
            </resource>
        </resources>
    </configuration>
</execution>

让我们了解如何使用Maven运行单元测试。

运行单元测试

如果我们考虑一下Maven构建的需求,我们会注意到在运行单元测试时必须考虑以下几点:

  • 只有当开发profile文件处于活跃状态时,才会运行单元测试。
  • 当我们运行单元测试时,我们的构建必须运行从名称不以前缀“IT”开头的测试类中找到的所有测试方法。

我们可以使用Maven Surefire插件来运行单元测试。我们可以按照以下步骤进行配置:

1. 如果skip.unit.tests属性的值为true,则将插件配置为跳过单元测试。

2. 将插件配置为忽略我们的集成测试。

我们可以通过将以下XML添加到POM文件的插件部分来配置Maven Surefire插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18</version>
    <configuration>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <skipTests>${skip.unit.tests}</skipTests>
        <!-- Excludes integration tests when unit tests are run -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>

我们可以使用以下命令运行单元测试:mvn-clean-test-P-dev

让我们继续了解如何使用Maven运行集成测试。

运行集成测试

如果我们考虑Maven构建的需求,我们会注意到在运行集成测试时必须考虑以下几点:

  • 只有当集成测试配置文件处于活动状态时,才会运行集成测试。
  • 如果集成测试失败,我们的构建也一定会失败。
  • 当我们运行集成测试时,我们的构建必须运行从名称以前缀“IT”开头的测试类中找到的所有测试方法。

我们可以使用Maven Failsafe插件(https://maven.apache.org/surefire/maven-failsafe-plugin/)运行集成测试。我们可以按照以下步骤进行配置:

1. 当我们运行集成测试时,创建一个调用集成测试并验证Maven Failsafe插件目标的执行。集成测试目标在Maven默认生命周期的集成测试阶段运行我们的集成测试。验证目标验证集成测试套件的结果,如果发现集成测试失败,则构建失败。

2. 如果skip.integration.tests属性的值为true,则将执行配置为跳过集成测试。

我们可以通过将以下XML添加到POM文件的插件部分来配置Maven Failsafe插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18</version>
    <executions>
        <!--
            Invokes both the integration-test and the verify goals of the
            Failsafe Maven plugin
        -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <!--
                    Skips integration tests if the value of skip.integration.tests
                    property is true
                -->
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

我们可以使用命令来运行集成测试:mvn-clean-verify-P integration-test

总结

  • 我们可以使用BuildHelper Maven插件将额外的源目录或资源目录添加到我们的Maven构建中。
  • 我们可以使用Maven Surefire插件来运行单元测试。
  • 我们可以使用Maven Failsafe插件运行集成测试。

测试代码地址:https://github.com/pkainulainen/maven-examples/tree/master/integration-testing

原文链接:https://www.petrikainulainen.net/programming/maven/integration-testing-with-maven

 

除特别注明外,本站所有文章均为老K的Java博客原创,转载请注明出处来自https://javakk.com/2905.html

关于

发表评论

表情 格式

暂无评论

登录

忘记密码 ?

切换登录

注册