此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Boot 4.0.4spring-doc.cadn.net.cn

测试

Spring Boot 提供了多种工具和注解,以帮助您测试应用程序。spring-doc.cadn.net.cn

测试支持由两个通用模块提供——spring-boot-test 包含核心组件,spring-boot-test-autoconfigure 为测试提供自动配置支持——以及多个专用的 -test 模块,用于为特定功能提供测试支持。spring-doc.cadn.net.cn

大多数开发者使用 spring-boot-starter-test 起步依赖,它不仅引入了通用的 Spring Boot 测试模块,还包含了 JUnit Jupiter、AssertJ、Hamcrest 以及其他许多有用的库,同时还引入了适用于其特定应用程序的专用 -test 模块。spring-doc.cadn.net.cn

如果你有使用 JUnit 4 编写的测试,可以使用 JUnit 6 的 Vintage 引擎来运行它们。 要使用 Vintage 引擎,请添加对 junit-vintage-engine 的依赖,如下例所示:spring-doc.cadn.net.cn

<dependency>
	<groupId>org.junit.vintage</groupId>
	<artifactId>junit-vintage-engine</artifactId>
	<scope>test</scope>
	<exclusions>
		<exclusion>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>

hamcrest-core 已被排除,转而使用作为 org.hamcrest:hamcrest 一部分的 spring-boot-starter-testspring-doc.cadn.net.cn