运行集成测试
虽然您可以从测试(或测试套件)本身非常轻松地启动 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 用于与应用程序通信。
默认情况下,插件使用 port9001 .
如果需要配置 JMX 端口,请参阅专用示例。 |
您还可以配置更高级的设置,以便在设置特定属性时跳过集成测试,请参阅专用示例。
在没有 Spring Boot 父 POM 的情况下使用故障保护
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:3.5.5
启动 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
自动部署的 MBean 的 JMX 名称,用于管理 Spring 应用程序的生命周期。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
jvmArguments
JVM 参数,这些参数应与用于运行应用程序的分叉进程相关联。在命令行上,确保在引号之间包装多个值。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
mainClass
主类的名称。如果未指定,将使用找到的第一个包含“main”方法的编译类。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
maxAttempts
检查 spring 应用程序是否准备就绪的最大尝试次数。结合“wait”参数,这将给出一个全局超时值(默认为 30 秒)
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
profiles
要激活的弹簧轮廓。指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行上,使用逗号分隔多个配置文件。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性列表。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
spring-boot:stop
例子
用于集成测试的随机端口
Spring Boot 测试集成的一个不错的功能是它可以为 Web 应用程序分配一个可用端口。
当start
目标,Spring Boot 应用程序是单独启动的,因此很难将实际端口传递给集成测试本身。
下面的示例展示了如何使用 Build Helper Maven 插件实现相同的功能:
<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