测试

Spring Boot 提供了许多实用工具和注解,以帮助测试你的应用。测试支持由两个模块提供:spring-boot-test 包含核心项,而 spring-boot-test-autoconfigure 支持测试的自动配置。

大多数开发者使用 spring-boot-starter-test starter,它导入了 Spring Boot 测试模块以及 JUnit Jupiter、AssertJ、Hamcrest 和许多其他有用的库。

如果你有使用 JUnit 4 的测试,可以使用 JUnit 5 的 vintage engine 来运行它们。要使用 vintage engine,请添加对 junit-vintage-engine 的依赖,如下例所示

<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-test 的一部分。