測試

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 引擎來執行它們。若要使用 vintage 引擎,請新增對 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 的一部分。