For the latest stable version, please use Spring Framework 6.2.0!spring-doc.cn

Ahead of Time Support for Tests

This chapter covers Spring’s Ahead of Time (AOT) support for integration tests using the Spring TestContext Framework.spring-doc.cn

The testing support extends Spring’s core AOT support with the following features.spring-doc.cn

  • Build-time detection of all integration tests in the current project that use the TestContext framework to load an ApplicationContext.spring-doc.cn

    • Provides explicit support for test classes based on JUnit Jupiter and JUnit 4 as well as implicit support for TestNG and other testing frameworks that use Spring’s core testing annotations — as long as the tests are run using a JUnit Platform TestEngine that is registered for the current project.spring-doc.cn

  • Build-time AOT processing: each unique test ApplicationContext in the current project will be refreshed for AOT processing.spring-doc.cn

  • Runtime AOT support: when executing in AOT runtime mode, a Spring integration test will use an AOT-optimized ApplicationContext that participates transparently with the context cache.spring-doc.cn

The @ContextHierarchy annotation is currently not supported in AOT mode.spring-doc.cn

To provide test-specific runtime hints for use within a GraalVM native image, you have the following options.spring-doc.cn

The TestRuntimeHintsRegistrar API serves as a companion to the core RuntimeHintsRegistrar API. If you need to register global hints for testing support that are not specific to particular test classes, favor implementing RuntimeHintsRegistrar over the test-specific API.spring-doc.cn

If you implement a custom ContextLoader, it must implement AotContextLoader in order to provide AOT build-time processing and AOT runtime execution support. Note, however, that all context loader implementations provided by the Spring Framework and Spring Boot already implement AotContextLoader.spring-doc.cn

If you implement a custom TestExecutionListener, it must implement AotTestExecutionListener in order to participate in AOT processing. See the SqlScriptsTestExecutionListener in the spring-test module for an example.spring-doc.cn