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

提前支持测试

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

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

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

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

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

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

@ContextHierarchyAOT 模式下当前不支持注释。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-test模块作为示例。spring-doc.cadn.net.cn