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

测试的预先编译支持

本章涵盖Spring TestContext框架对使用提前编译(AOT)的集成测试的支持。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 PlatformTestEngine运行。spring-doc.cadn.net.cn

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

  • 运行时AOT支持:在AOT运行时模式下执行时,Spring集成测试将使用经过AOT优化的ApplicationContext,该优化器可与上下文缓存无缝协作。spring-doc.cadn.net.cn

@ContextHierarchy 注解目前在 AOT 模式下不受支持。spring-doc.cadn.net.cn

要为GraalVM本机镜像中的使用提供测试专用的运行时提示,可使用以下选项。spring-doc.cadn.net.cn

TestRuntimeHintsRegistrar API 作为核心 RuntimeHintsRegistrar API 的配套接口。若需为测试支持注册不特定于特定测试类的全局提示,优先选择实现 RuntimeHintsRegistrar 而非该测试专用 API。spring-doc.cadn.net.cn

如果您实现自定义的 ContextLoader,则必须实现 AotContextLoader 接口, 才能提供 AOT 构建时处理和 AOT 运行时执行支持。但请注意, Spring Framework 和 Spring Boot 提供的所有上下文加载器实现均已实现 AotContextLoaderspring-doc.cadn.net.cn

如果您实现了一个自定义的 TestExecutionListener,它必须实现 AotTestExecutionListener 才能参与AOT处理。有关示例,请参见 spring-test 模块中的 SqlScriptsTestExecutionListenerspring-doc.cadn.net.cn