跳到主要内容

测试

DeepSeek V3 中英对照 Testing

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

大多数开发者使用 spring-boot-starter-test 启动器,它导入了 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>
xml

hamcrest-core 已被排除,转而使用 spring-boot-starter-test 中包含的 org.hamcrest:hamcrest

章节摘要