此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
虽然您可以非常轻松地从测试(或测试套件)本身启动 Spring Boot 应用程序,但可能需要在构建本身中处理它。
若要确保围绕集成测试正确管理 Spring Boot 应用程序的生命周期,可以使用 and 目标,如以下示例所示: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>
此类设置现在可以使用故障保护插件来运行集成测试,如您所料。
应用程序在单独的进程中启动,JMX 用于与应用程序通信。
默认情况下,插件使用 port .
如果需要配置 JMX 端口,请参阅专用示例。9001 |
您还可以配置更高级的设置,以便在设置特定属性时跳过集成测试,请参阅专用示例。
应用程序在单独的进程中启动,JMX 用于与应用程序通信。
默认情况下,插件使用 port .
如果需要配置 JMX 端口,请参阅专用示例。9001 |
在没有 Spring Boot 的父 POM 的情况下使用故障保护
Spring Boot 的父 POM 将 Failsafe 配置为 .
如果没有此配置,这会导致 Failsafe 使用已编译的类而不是重新打包的 jar,因此 Failsafe 无法加载应用程序的类。
如果不使用父 POM,则应以相同的方式配置故障保护,如以下示例所示:spring-boot-starter-parent
<classesDirectory>
${project.build.outputDirectory}
<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.3.2-SNAPSHOT
开始春季申请。与目标相反,这不会阻止并允许其他目标在应用程序上运行。此目标通常用于集成测试方案,其中应用程序在测试套件之前启动,之后停止。run
可选参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
参数详细信息
addResources
将 maven 资源直接添加到类路径中,这样就可以对资源进行实时就地编辑。重复的资源将被删除,以防止它们在被调用时出现两次。请考虑添加到您的项目中,因为它提供了此功能以及更多功能。target/classes
ClassLoader.getResources()
spring-boot-devtools
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
additionalClasspathElements
应添加到类路径的其他类路径元素。元素可以是包含类和资源的目录,也可以是 jar 文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
classesDirectory
包含应用于运行应用程序的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
commandlineArguments
应传递给应用程序的命令行中的参数。使用空格分隔多个参数,并确保在引号之间换行多个值。指定时,优先于 。#arguments
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
directories
包含应添加到类路径中的类或资源的其他目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
environmentVariables
应与用于运行应用程序的分叉进程关联的环境变量列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Exclude
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
includes
要包含的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Include
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
jmxName
自动部署的 MBean 的 JMX 名称,用于管理 Spring 应用程序的生命周期。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
jvmArguments
应与用于运行应用程序的分叉进程相关联的 JVM 参数。在命令行中,确保在引号之间换行多个值。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
mainClass
主类的名称。如果未指定,则将使用找到的第一个包含“main”方法的编译类。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
maxAttempts
检查弹簧应用程序是否准备就绪的最大尝试次数。结合“wait”参数,这将给出一个全局超时值(默认为 30 秒)
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
profiles
要激活的弹簧轮廓。指定“spring.profiles.active”参数的便捷快捷方式。在命令行上,使用逗号分隔多个配置文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 | 类型 | 违约 |
---|---|---|
|
|
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
spring-boot:stop
org.springframework.boot:spring-boot-maven-plugin:3.3.2-SNAPSHOT
停止由“启动”目标启动的应用程序。通常在测试套件完成后调用。
可选参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
例子
用于集成测试的随机端口
Spring Boot 测试集成的一个很好的功能是它可以为 Web 应用程序分配一个空闲端口。
当使用插件的目标时,Spring Boot 应用程序是单独启动的,因此很难将实际端口传递给集成测试本身。start
下面的示例展示了如何使用 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 端口
该属性允许自定义插件用于与 Spring Boot 应用程序通信的端口。jmxPort
此示例演示如何在已使用的情况下自定义端口: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 端口,请确保在全局配置中执行此操作,如上所示,以便两个目标共享该端口。 |
跳过集成测试
该属性允许完全跳过 Spring Boot maven 插件的执行。skip
此示例演示如何使用命令行属性跳过集成测试,同时仍确保目标运行: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
如果需要配置 JMX 端口,请确保在全局配置中执行此操作,如上所示,以便两个目标共享该端口。 |