測試

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