跳到主要内容

运行集成测试

DeepSeek V3 中英对照 Running Integration Tests

虽然你可以很容易地从你的测试(或测试套件)本身启动你的 Spring Boot 应用程序,但可能更希望在构建过程中处理这一点。为了确保你的 Spring Boot 应用程序的生命周期在集成测试中得到妥善管理,你可以使用 startstop 目标,如下例所示:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
xml

这种设置现在可以使用 failsafe-plugin 来运行你的集成测试,正如你所期望的那样。

备注

应用程序在一个单独的进程中启动,并使用 JMX 与应用程序进行通信。默认情况下,插件使用端口 9001。如果需要配置 JMX 端口,请参见专用示例

你也可以配置一个更高级的设置,以便在设置了特定属性时跳过集成测试,请参阅专门的示例

不使用 Spring Boot 父 POM 的 Failsafe 集成测试

Spring Boot 的父 POM spring-boot-starter-parent 将 Failsafe 的 <classesDirectory> 配置为 ${project.build.outputDirectory}。如果没有此配置,Failsafe 将使用编译后的类而不是重新打包的 jar,这将导致 Failsafe 无法加载应用程序的类。如果你没有使用父 POM,你应该按照以下示例的方式配置 Failsafe:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
</configuration>
</plugin>
xml

spring-boot:start

org.springframework.boot:spring-boot-maven-plugin:3.4.2

启动一个 Spring 应用程序。与 run 目标不同,此操作不会阻塞,并允许其他目标在应用程序上操作。此目标通常用于集成测试场景中,在测试套件执行之前启动应用程序,并在之后停止。

必填参数

名称类型默认值
classesDirectoryFile${project.build.outputDirectory}

可选参数

名称类型默认值
addResourcesbooleanfalse
additionalClasspathElementsString[]
agentsFile[]
argumentsString[]
commandlineArgumentsString
environmentVariablesMap
excludeGroupIdsString
excludesList
includesList
jmxNameStringorg.springframework.boot:type=Admin,name=SpringApplication
jmxPortint9001
jvmArgumentsString
mainClassString
maxAttemptsint60
noverifyboolean
profilesString[]
skipbooleanfalse
systemPropertyVariablesMap
useTestClasspathBooleanfalse
waitlong500
workingDirectoryFile

参数详情

addResources

将 Maven 资源直接添加到类路径中,这样可以在不重新构建的情况下实时编辑资源。为了防止在调用 ClassLoader.getResources() 时资源重复出现,target/classes 中的重复资源会被移除。建议您考虑在项目中添加 spring-boot-devtools,因为它不仅提供了此功能,还提供了更多其他功能。

名称addResources
类型boolean
默认值false
用户属性spring-boot.run.addResources
1.0.0 版本起

additionalClasspathElements

应添加到类路径中的额外类路径元素。一个元素可以是包含类和资源的目录,也可以是一个 jar 文件。

名称additionalClasspathElements
类型java.lang.String[]
默认值
用户属性spring-boot.run.additional-classpath-elements
自版本3.2.0

agents

代理 jar 文件的路径。

Nameagents
Typejava.io.File[]
Default value
User propertyspring-boot.run.agents
Since2.2.0

arguments

应传递给应用程序的参数。

名称arguments
类型java.lang.String[]
默认值
用户属性
自版本1.0.0

classesDirectory

包含用于运行应用程序的类和资源文件的目录。

名称classesDirectory
类型java.io.File
默认值${project.build.outputDirectory}
用户属性
自版本1.0.0

commandlineArguments

应从命令行传递给应用程序的参数。使用空格分隔多个参数,并确保将多个值用引号括起来。指定此选项时,其优先级高于 #arguments

名称commandlineArguments
类型java.lang.String
默认值
用户属性spring-boot.run.arguments
自版本2.2.3

environmentVariables

与用于运行应用程序的分叉进程应关联的环境变量列表。

名称environmentVariables
类型java.util.Map
默认值
用户属性
自版本2.1.0

excludeGroupIds

排除的 groupId 名称的逗号分隔列表(精确匹配)。

名称excludeGroupIds
类型java.lang.String
默认值
用户属性spring-boot.excludeGroupIds
自版本1.1.0

excludes

要排除的构件定义集合。Exclude 元素定义了必需的 groupIdartifactId 组件以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称excludes
类型java.util.List
默认值
用户属性spring-boot.excludes
自版本1.1.0

includes

要包含的构件定义的集合。Include 元素定义了必需的 groupIdartifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称includes
类型java.util.List
默认值
用户属性spring-boot.includes
自版本1.2.0

jmxName

自动部署的 MBean 管理 Spring 应用程序生命周期的 JMX 名称。

名称jmxName
类型java.lang.String
默认值org.springframework.boot:type=Admin,name=SpringApplication
用户属性
起始版本

jmxPort

用于暴露平台 MBeanServer 的端口。

名称jmxPort
类型int
默认值9001
用户属性
自版本

jvmArguments

用于运行应用程序的派生进程应关联的 JVM 参数。在命令行上,请确保将多个值用引号括起来。

名称jvmArguments
类型java.lang.String
默认值
用户属性spring-boot.run.jvmArguments
自版本1.1.0

mainClass

主类的名称。如果未指定,将使用包含 main 方法的第一个编译类。

名称mainClass
类型java.lang.String
默认值
用户属性spring-boot.run.main-class
自版本1.0.0

maxAttempts

检查 Spring 应用是否准备就绪的最大尝试次数。与 wait 参数结合使用,这将给出一个全局的超时值(默认 30 秒)。

名称maxAttempts
类型int
默认值60
用户属性spring-boot.start.maxAttempts
自版本

noverify

用于指示代理需要 -noverify 的标记。

名称noverify
类型boolean
默认值
用户属性spring-boot.run.noverify
1.0.0 版本起

profiles

要激活的 Spring profiles。这是指定 spring.profiles.active 参数的便捷快捷方式。在命令行中使用逗号分隔多个 profiles。

名称profiles
类型java.lang.String[]
默认值
用户属性spring-boot.run.profiles
自版本1.3.0

skip

跳过执行。

名称skip
类型boolean
默认值false
用户属性spring-boot.run.skip
自版本1.3.2

systemPropertyVariables

传递给进程的 JVM 系统属性列表。

名称systemPropertyVariables
类型java.util.Map
默认值
用户属性
自版本2.1.0

useTestClasspath

在运行时包含测试类路径的标志。

名称useTestClasspath
类型java.lang.Boolean
默认值false
用户属性spring-boot.run.useTestClasspath

wait

每次检查 Spring 应用是否就绪的尝试之间的等待时间(以毫秒为单位)。

名称wait
类型long
默认值500
用户属性spring-boot.start.wait
自版本

workingDirectory

应用程序使用的当前工作目录。如果未指定,将使用 basedir

名称workingDirectory
类型java.io.File
默认值
用户属性spring-boot.run.workingDirectory
自版本1.5.0

spring-boot:stop

org.springframework.boot:spring-boot-maven-plugin:3.4.2

停止由 "start" 目标启动的应用程序。通常在测试套件完成后调用。

可选参数

名称类型默认值
jmxNameStringorg.springframework.boot:type=Admin,name=SpringApplication
jmxPortint9001
skipbooleanfalse

参数详情

jmxName

自动部署的 MBean 管理应用程序生命周期的 JMX 名称。

名称jmxName
类型java.lang.String
默认值org.springframework.boot:type=Admin,name=SpringApplication
用户属性
自版本

jmxPort

用于查找平台 MBeanServer 的端口。

名称jmxPort
类型int
默认值9001
用户属性
起始版本

skip

跳过执行。

名称skip
类型boolean
默认值false
用户属性spring-boot.stop.skip
自版本1.3.2

示例

集成测试的随机端口

Spring Boot 测试集成的一个很好的特性是,它可以为 Web 应用程序分配一个空闲端口。当使用插件的 start 目标时,Spring Boot 应用程序是单独启动的,这使得将实际端口传递给集成测试本身变得困难。

下面的示例展示了如何使用 Build Helper Maven Plugin 实现相同的功能:

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-tomcat-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>tomcat.http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<arguments>
<argument>--server.port=${tomcat.http.port}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<test.server.port>${tomcat.http.port}</test.server.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
xml

现在,你可以在任何集成测试中检索 test.server.port 系统属性,以创建到服务器的正确 URL

自定义 JMX 端口

jmxPort 属性允许自定义插件用于与 Spring Boot 应用程序通信的端口。

此示例展示了在端口 9001 已被占用的情况下,如何自定义端口:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jmxPort>9009</jmxPort>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
xml

:::提示
如果需要配置 JMX 端口,请确保按照上述方式在全局配置中进行设置,以便两个目标共享该配置。
:::

跳过集成测试

skip 属性允许完全跳过 Spring Boot Maven 插件的执行。

此示例展示了如何通过命令行属性跳过集成测试,同时确保 repackage 目标仍然运行:

<project>
<properties>
<skip.it>false</skip.it>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<skip>${skip.it}</skip>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<skip>${skip.it}</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>${skip.it}</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
xml

默认情况下,集成测试将会运行,但此设置允许你通过以下方式在命令行中轻松禁用它们:

$ mvn verify -Dskip.it=true
shell