运行集成测试
虽然你可以很容易地从测试(或测试套件)启动 Spring Boot 应用,但最好是在构建中处理这些。
为了确保你的 Spring Boot 应用的生命周期围绕集成测试得到妥善管理,你可以使用开始和停目标,如下例所示:
<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插件来运行集成测试,正如你预期的那样。
应用程序在独立进程中启动,JMX 用于与应用程序通信。
默认情况下,插件使用 port9001.
如果你需要配置JMX端口,请参见专门的示例。 |
你也可以配置更高级的设置,当特定属性被设置时跳过集成测试,详见专门的示例。
使用无 Spring Boot 父 POM 的 Failsafe
春靴的母POM,Spring靴启动父, 配置了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:开始
org.springframework.boot:spring-boot-maven-plugin:4.0.0
开始春季施药。与执行目标,这不会阻塞,允许其他目标在应用程序上运行。该目标通常用于集成测试场景,即应用程序在测试套件前启动,测试后停止。
可选参数
| 名称 | 类型 | 默认值 |
|---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
参数细节
addResources
直接将 maven 资源添加到类路径中,这允许资源的现场编辑。重复的资源会从目标/职业以防止它们重复出现,如果ClassLoader.getResources()被叫去。请考虑添加Spring-boot-devtools而是你的项目,因为它提供了这个功能以及更多功能。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
additionalClasspathElements
需要添加到类路径中的额外类路径元素。元素可以是包含类和资源的目录,也可以是 jar 文件。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
classes目录
包含运行应用的类和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
命令行参数
命令行的参数,应该传递给应用程序。用空格分隔多个参数,并确保在引号之间包裹多个值。在指定时,优先于#arguments.
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
environmentVariables
运行应用程序时应关联到用于运行应用的分支进程的环境变量列表。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
exclusionGroupIds
逗号分隔的groupid名称列表,排除(完全匹配)。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
排除
排除的工件定义集合。这排除元素定义了强制性组ID和artifactId(遗物ID组件和可选分类元件。当配置为属性时,值应以逗号分隔,并用冒号分隔:groupId:artifactId,groupId:artifactId:分类器
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
包括
包含工件定义的集合。这包括元素定义了强制性组ID和artifactId(遗物ID组件和可选分类元件。当配置为属性时,值应以逗号分隔,并用冒号分隔:groupId:artifactId,groupId:artifactId:分类器
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
jmxName(jmx名称)
JMX 名称是自动部署的 MBean 管理 Spring 应用生命周期的名称。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
jvm论证
JVM参数应与用于运行应用的分支进程关联。在命令行中,确保引号之间包裹多个值。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
mainClass
主类的名字。如果未指定,将使用第一个编译类,包含“main”方法。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
最大尝试次数
检查弹药申请是否准备好的最大尝试次数。结合“等待”参数,这给出了一个全局超时值(默认为30秒)
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
配置 文件
Spring轮廓要启动。方便快捷方式,只需指定“spring.profiles.active”参数。在命令行用逗号分隔多个配置文件。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
systemPropertyVariables
传递给进程的JVM系统属性列表。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
Spring靴:停止
例子
积分测试的随机端口
Spring Boot测试集成的一个优点是可以为网页应用分配一个空闲端口。
当开始插件的目标是单独启动的,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系统属性在你的任何集成测试中都用来创建一个合适的网址去服务器。
自定义JMX端口
这jmxPortproperty 允许自定义插件与 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端口,确保按照上面所示的全局配置,这样两个目标都能共享。 |
跳过积分测试
这跳属性允许完全跳过 Spring Boot maven 插件的执行。
这个例子展示了如何通过命令行属性跳过集成测试,同时仍然确保重新包装进球跑:
<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