|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Boot 4.0.4! |
运行集成测试
虽然您可以非常轻松地从测试(或测试套件)本身启动 Spring Boot 应用程序,但可能更希望在构建过程中处理该操作。
为了确保在集成测试期间正确管理 Spring Boot 应用程序的生命周期,您可以使用 start 和 stop 目标,如下例所示:
<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>
现在可以使用 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>
spring-boot:start
org.springframework.boot:spring-boot-maven-plugin:4.1.0-M3
启动一个 Spring 应用程序。与 run 目标相反,此操作不会阻塞,并允许其他目标对该应用程序进行操作。此目标通常用于集成测试场景,其中应用程序在测试套件之前启动,并在之后停止。
可选参数
| 名称 | 类型 | 默认 |
|---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
参数详情
addResources
将 Maven 资源直接添加到类路径中,这样可以对资源进行实时原地编辑。重复的资源会从 target/classes 中移除,以防止在调用 ClassLoader.getResources() 时它们出现两次。请考虑改为向您的项目添加 spring-boot-devtools,因为它提供了此功能以及更多其他功能。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
additionalClasspathElements
应添加到类路径中的其他类路径元素。元素可以是包含类和资源的目录,也可以是 jar 文件。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
classesDirectory
包含用于运行应用程序的类和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
commandlineArguments
应从命令行传递给应用程序的参数。使用空格分隔多个参数,并确保将多个值用引号括起来。指定时,优先于 #arguments。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
environmentVariables
应与用于运行应用程序的派生进程关联的环境变量列表。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
excludeGroupIds
要排除的 groupId 名称列表(精确匹配),以逗号分隔。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
excludes
要排除的构件定义集合。第 Exclude 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
includes
要包含的构件定义集合。第 Include 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
jmxName
管理 Spring 应用程序生命周期的自动部署 MBean 的 JMX 名称。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
jvmArguments
应与用于运行应用程序的派生进程关联的 JVM 参数。在命令行上,请确保将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
mainClass
主类的名称。如果未指定,将使用找到的第一个包含“main”方法的已编译类。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
maxAttempts
检查 Spring 应用程序是否就绪的最大尝试次数。与“wait”参数结合使用时,这将给出一个全局超时值(默认为 30 秒)
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
profiles
要激活的 Spring 配置文件。这是指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行上,使用逗号分隔多个配置文件。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性列表。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
spring-boot:stop
org.springframework.boot:spring-boot-maven-plugin:4.1.0-M3
停止由“start”目标启动的应用程序。通常在测试套件完成后调用一次。
可选参数
| 名称 | 类型 | 默认 |
|---|---|---|
|
|
|
|
|
|
|
|
示例
集成测试的随机端口
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>
您现在可以在任何集成测试中检索 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>
| 如果您需要配置 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>
默认情况下,集成测试将会运行,但此设置允许您通过命令行轻松地禁用它们,如下所示:
$ mvn verify -Dskip.it=true