运行集成测试
虽然你可以非常轻松地从测试(或测试套件)本身启动你的 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 的 Parent POM 使用 Failsafe
Spring Boot 的 Parent POM,即 spring-boot-starter-parent
,将 Failsafe 的 <classesDirectory>
配置为 ${project.build.outputDirectory}
。没有这个配置(它使 Failsafe 使用编译后的类而不是重新打包的 jar),Failsafe 无法加载你的应用类。如果你没有使用 parent 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:3.4.5
启动一个 spring 应用。与 run
目标不同,这个目标不会阻塞,并允许其他目标操作应用。此目标通常用于集成测试场景,在测试套件之前启动应用并在之后停止。
可选参数
名称 | 类型 | 默认值 |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
参数详情
addResources
直接将 Maven 资源添加到 classpath,这允许对资源进行实时就地编辑。重复的资源会从 target/classes
中移除,以防止在调用 ClassLoader.getResources()
时出现两次。请考虑在你的项目中添加 spring-boot-devtools
,因为它提供了此功能以及更多其他功能。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
additionalClasspathElements
应添加到 classpath 的额外 classpath 元素。元素可以是包含类和资源的目录或 jar 文件。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
classesDirectory
包含应用于运行应用的类文件和资源文件的目录。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
commandlineArguments
应传递给应用的命令行参数。使用空格分隔多个参数,并确保将多个值用引号括起来。指定时,优先级高于 #arguments
。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
environmentVariables
应与用于运行应用的分叉进程关联的环境变量列表。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
excludeGroupIds
要排除的 groupId 名称列表(逗号分隔,精确匹配)。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
excludes
要排除的 artifact 定义集合。Exclude
元素定义了必需的 groupId
和 artifactId
组件以及可选的 classifier
组件。配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
includes
要包含的 artifact 定义集合。Include
元素定义了必需的 groupId
和 artifactId
组件以及可选的 classifier
组件。配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
jmxName
管理 spring 应用生命周期的自动部署 MBean 的 JMX 名称。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
jvmArguments
应与用于运行应用的分叉进程关联的 JVM 参数。在命令行上,确保将多个值用引号括起来。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
mainClass
主类的名称。如果未指定,则使用找到的第一个包含 'main' 方法的编译类。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
maxAttempts
检查 spring 应用是否就绪的最大尝试次数。与 "wait" 参数结合使用,这会提供一个全局超时值(默认为 30 秒)。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
profiles
要激活的 spring profile。这是指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行上,使用逗号分隔多个 profile。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性列表。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
spring-boot:stop
示例
集成测试的随机端口
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