对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

对测试的提前支持

本章介绍了 Spring 对使用 Spring TestContext 框架。spring-doc.cadn.net.cn

testing 支持扩展了 Spring 的核心 AOT 支持,其中包含 以下功能。spring-doc.cadn.net.cn

  • 当前项目中所有使用 TestContext 框架加载ApplicationContext.spring-doc.cadn.net.cn

    • 为基于 JUnit Jupiter 和 JUnit 4 的测试类提供显式支持 作为对 TestNG 和其他使用 Spring 核心的测试框架的隐式支持 testing 注解 — 只要测试是使用 JUnit Platform 运行的TestEngine,该 ID 已为当前项目注册。spring-doc.cadn.net.cn

  • 构建时 AOT 处理:每个测试都是唯一的ApplicationContext在当前项目中 将刷新以进行 AOT 处理spring-doc.cadn.net.cn

  • 运行时 AOT 支持:在 AOT 运行时模式下执行时,Spring 集成测试将 使用 AOT 优化ApplicationContext以透明方式参与上下文缓存spring-doc.cadn.net.cn

@ContextHierarchyAOT 模式当前不支持 annotation。spring-doc.cadn.net.cn

要提供特定于测试的运行时提示以在 GraalVM 原生映像中使用,您需要 以下选项。spring-doc.cadn.net.cn

TestRuntimeHintsRegistrarAPI 作为核心的伴侣RuntimeHintsRegistrar应用程序接口。如果需要注册全局提示以测试支持 不特定于特定测试类,则有利于实现RuntimeHintsRegistrar通过特定于测试的 API。spring-doc.cadn.net.cn

如果您实现自定义ContextLoader,它必须实现AotContextLoader在 order 提供 AOT 构建时处理和 AOT 运行时执行支持。注意 但是,Spring Framework 和 Spring Boot 已经实现了AotContextLoader.spring-doc.cadn.net.cn

如果您实现自定义TestExecutionListener,它必须实现AotTestExecutionListener以便参与 AOT 处理。请参阅SqlScriptsTestExecutionListener在 这spring-testmodule 为例。spring-doc.cadn.net.cn