运行集成测试

虽然您可以很容易地从测试(或测试套件)本身启动您的 Spring Boot 应用程序,但可能希望在构建本身中处理它。为了确保您的 Spring Boot 应用程序的生命周期在集成测试周围得到正确管理,您可以使用 startstop 目标,如以下示例所示:

<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 将无法加载您的应用程序类,因为它会导致 Failsafe 使用已编译的类而不是重新打包的 jar。如果您不使用父 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:4.0.0

启动一个 Spring 应用程序。与 run 目标相反,这不会阻塞,并允许其他目标在应用程序上运行。此目标通常用于集成测试场景,其中应用程序在测试套件之前启动并在之后停止。

所需参数

名称 类型 默认值

classesDirectory

文件

${project.build.outputDirectory}

可选参数

名称 类型 默认值

addResources

布尔值

additionalClasspathElements

String[]

agents

File[]

arguments

String[]

commandlineArguments

字符串

environmentVariables

Map

excludeGroupIds

字符串

excludes

List

includes

List

jmxName

字符串

org.springframework.boot:type=Admin,name=SpringApplication

jmxPort

int

9001

jvmArguments

字符串

mainClass

字符串

maxAttempts

int

60

noverify

布尔值

profiles

String[]

skip

布尔值

systemPropertyVariables

Map

useTestClasspath

布尔值

wait

long

500

workingDirectory

文件

参数详情

addResources

直接将 Maven 资源添加到类路径,这允许实时原地编辑资源。重复的资源将从 target/classes 中移除,以防止在调用 ClassLoader.getResources() 时它们出现两次。请考虑将 spring-boot-devtools 添加到您的项目中,因为它提供了此功能以及更多功能。

名称

addResources

类型

布尔值

默认值

用户属性

spring-boot.run.addResources

1.0.0

additionalClasspathElements

应添加到类路径的额外类路径元素。一个元素可以是包含类和资源的目录,也可以是 jar 文件。

名称

additionalClasspathElements

类型

java.lang.String[]

默认值

用户属性

spring-boot.run.additional-classpath-elements

3.2.0

agents

代理 jar 的路径。

名称

agents

类型

java.io.File[]

默认值

用户属性

spring-boot.run.agents

2.2.0

arguments

应传递给应用程序的参数。

名称

arguments

类型

java.lang.String[]

默认值

用户属性

1.0.0

classesDirectory

包含应用于运行应用程序的类和资源文件的目录。

名称

classesDirectory

类型

java.io.File

默认值

${project.build.outputDirectory}

用户属性

1.0.0

commandlineArguments

应传递给应用程序的命令行参数。使用空格分隔多个参数,并确保将多个值用引号括起来。当指定时,优先于 #arguments

名称

commandlineArguments

类型

java.lang.String

默认值

用户属性

spring-boot.run.arguments

2.2.3

environmentVariables

应与用于运行应用程序的分叉进程关联的环境变量列表。

名称

environmentVariables

类型

java.util.Map

默认值

用户属性

2.1.0

excludeGroupIds

要排除的 groupId 名称的逗号分隔列表(精确匹配)。

名称

excludeGroupIds

类型

java.lang.String

默认值

用户属性

spring-boot.excludeGroupIds

1.1.0

excludes

要排除的 artifact 定义集合。Exclude 元素定义了强制的 groupIdartifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称

excludes

类型

java.util.List

默认值

用户属性

spring-boot.excludes

1.1.0

includes

要包含的 artifact 定义集合。Include 元素定义了强制的 groupIdartifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称

includes

类型

java.util.List

默认值

用户属性

spring-boot.includes

1.2.0

jmxName

自动部署的 MBean 的 JMX 名称,用于管理 Spring 应用程序的生命周期。

名称

jmxName

类型

java.lang.String

默认值

org.springframework.boot:type=Admin,name=SpringApplication

用户属性

jmxPort

用于暴露平台 MBeanServer 的端口。

名称

jmxPort

类型

int

默认值

9001

用户属性

jvmArguments

应与用于运行应用程序的分叉进程关联的 JVM 参数。在命令行上,请确保将多个值用引号括起来。

名称

jvmArguments

类型

java.lang.String

默认值

用户属性

spring-boot.run.jvmArguments

1.1.0

mainClass

主类的名称。如果未指定,将使用找到的第一个包含“main”方法的已编译类。

名称

mainClass

类型

java.lang.String

默认值

用户属性

spring-boot.run.main-class

1.0.0

maxAttempts

检查 Spring 应用程序是否准备就绪的最大尝试次数。与“wait”参数结合使用,这将给出一个全局超时值(默认为 30 秒)

名称

maxAttempts

类型

int

默认值

60

用户属性

spring-boot.start.maxAttempts

noverify

指示代理需要 -noverify 的标志。

名称

noverify

类型

布尔值

默认值

用户属性

spring-boot.run.noverify

1.0.0

profiles

要激活的 Spring 配置文件。指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行上使用逗号分隔多个配置文件。

名称

profiles

类型

java.lang.String[]

默认值

用户属性

spring-boot.run.profiles

1.3.0

skip

跳过执行。

名称

skip

类型

布尔值

默认值

用户属性

spring-boot.run.skip

1.3.2

systemPropertyVariables

要传递给进程的 JVM 系统属性列表。

名称

systemPropertyVariables

类型

java.util.Map

默认值

用户属性

2.1.0

useTestClasspath

运行时代包含测试类路径的标志。

名称

useTestClasspath

类型

布尔值

默认值

用户属性

spring-boot.run.useTestClasspath

wait

每次尝试检查 Spring 应用程序是否准备就绪之间等待的毫秒数。

名称

wait

类型

long

默认值

500

用户属性

spring-boot.start.wait

workingDirectory

应用程序使用的当前工作目录。如果未指定,将使用基于目录。

名称

workingDirectory

类型

java.io.File

默认值

用户属性

spring-boot.run.workingDirectory

1.5.0

spring-boot:stop

org.springframework.boot:spring-boot-maven-plugin:4.0.0

停止已由“start”目标启动的应用程序。通常在测试套件完成后调用。

可选参数

名称 类型 默认值

jmxName

字符串

org.springframework.boot:type=Admin,name=SpringApplication

jmxPort

int

9001

skip

布尔值

参数详情

jmxName

管理应用程序生命周期的自动部署 MBean 的 JMX 名称。

名称

jmxName

类型

java.lang.String

默认值

org.springframework.boot:type=Admin,name=SpringApplication

用户属性

jmxPort

用于查找平台 MBeanServer 的端口。

名称

jmxPort

类型

int

默认值

9001

用户属性

skip

跳过执行。

名称

skip

类型

布尔值

默认值

用户属性

spring-boot.stop.skip

1.3.2

示例

集成测试的随机端口

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
© . This site is unofficial and not affiliated with VMware.