运行集成测试
虽然您可以很容易地从测试(或测试套件)本身启动 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:3.4.6
Start a spring application. Contrary to the run
goal, this does not block and allows other goals to operate on the application. This goal is typically used in integration test scenario where the application is started before a test suite and stopped after.
Optional parameters
Name | Type | Default |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
|
|
|
||
|
||
|
|
|
|
||
|
||
|
|
|
|
||
|
|
|
|
|
|
|
Parameter details
addResources
Add maven resources to the classpath directly, this allows live in-place editing of resources. Duplicate resources are removed from target/classes
to prevent them from appearing twice if ClassLoader.getResources()
is called. Please consider adding spring-boot-devtools
to your project instead as it provides this feature and many more.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
additionalClasspathElements
Additional classpath elements that should be added to the classpath. An element can be a directory with classes and resources or a jar file.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
agents
Path to agent jars.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
arguments
Arguments that should be passed to the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
classesDirectory
Directory containing the classes and resource files that should be used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
commandlineArguments
Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over #arguments
.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
environmentVariables
List of Environment variables that should be associated with the forked process used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludeGroupIds
Comma separated list of groupId names to exclude (exact match).
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludes
Collection of artifact definitions to exclude. The Exclude
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
includes
Collection of artifact definitions to include. The Include
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
jmxName
The JMX name of the automatically deployed MBean managing the lifecycle of the spring application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
jmxPort
The port to use to expose the platform MBeanServer.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
jvmArguments
JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
mainClass
The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
maxAttempts
The maximum number of attempts to check if the spring application is ready. Combined with the "wait" argument, this gives a global timeout value (30 sec by default)
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
noverify
Flag to say that the agent requires -noverify.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
profiles
The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
skip
Skip the execution.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
systemPropertyVariables
List of JVM system properties to pass to the process.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
useTestClasspath
Flag to include the test classpath when running.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
spring-boot:stop
org.springframework.boot:spring-boot-maven-plugin:3.4.6
Stop an application that has been started by the "start" goal. Typically invoked once a test suite has completed.
Optional parameters
Name | Type | Default |
---|---|---|
|
|
|
|
|
|
|
|
Parameter details
jmxName
The JMX name of the automatically deployed MBean managing the lifecycle of the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
示例
集成测试的随机端口
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