Ahead of Time Support for Tests
-
构建时 AOT 处理每个独特的测试 ApplicationContext。
-
运行时 AOT 支持,集成测试将使用参与上下文字符串的 AOT 优化 ApplicationContext。
本章介绍了 Spring Ahead of Time (AOT) 对使用 Spring TestContext Framework 的集成测试的支持。
This chapter covers Spring’s Ahead of Time (AOT) support for integration tests using the Spring TestContext Framework.
测试支持扩展了 Spring 的 core AOT support,添加了以下功能。
The testing support extends Spring’s core AOT support with the following features.
-
Build-time detection of all integration tests in the current project that use the TestContext framework to load an
ApplicationContext
.-
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.
-
-
Build-time AOT processing: each unique test
ApplicationContext
in the current project will be refreshed for AOT processing. -
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.
默认情况下,所有测试都在 AOT 模式下启用。但是,您可以在 AOT 模式下通过使用 @DisabledInAotMode
进行注释来有选择地禁用整个测试类或单个测试方法。在使用 JUnit Jupiter 时,您可以通过 Jupiter 的 @EnabledInNativeImage
和 @DisabledInNativeImage
注释有选择地在 GraalVM nativeimage 中启用或禁用测试。请注意, @DisabledInAotMode
在运行 GraalVM nativeimage 时还会禁用带注释的测试类或测试方法,类似于 JUnit Jupiter 的 @DisabledInNativeImage
注释。
All tests are enabled in AOT mode by default. However, you can selectively disable an
entire test class or individual test method in AOT mode by annotating it with
@DisabledInAotMode
.
When using JUnit Jupiter, you may selectively enable or disable tests in a GraalVM native
image via Jupiter’s @EnabledInNativeImage
and @DisabledInNativeImage
annotations.
Note that @DisabledInAotMode
also disables the annotated test class or test method when
running within a GraalVM native image, analogous to JUnit Jupiter’s
@DisabledInNativeImage
annotation.
默认情况下,如果在构建时 AOT 处理期间遇到错误,将抛出异常,整个进程将立即失败。 By default, if an error is encountered during build-time AOT processing, an exception will be thrown, and the overall process will fail immediately. 如果您希望在遇到错误后继续构建时 AOT 处理,您可以禁用 If you would prefer that build-time AOT processing continue after errors are encountered,
you can disable the 可以通过将名为`spring.test.aot.processing.failOnError`的JVM系统属性设置为`false`来通过命令行或构建脚本禁用`failOnError`模式。另外,还可以通过 The |
AOT 模式不支持 The |
要为 GraalVM 原生映像中使用提供特定于测试的运行时提示,您可以选择以下选项。
To provide test-specific runtime hints for use within a GraalVM native image, you have the following options.
-
Implement a custom
TestRuntimeHintsRegistrar
and register it globally viaMETA-INF/spring/aot.factories
. -
Implement a custom
RuntimeHintsRegistrar
and register it globally viaMETA-INF/spring/aot.factories
or locally on a test class via@ImportRuntimeHints
. -
Annotate a test class with
@Reflective
or@RegisterReflectionForBinding
. -
See Runtime Hints for details on Spring’s core runtime hints and annotation support.
The |
如果您实现了一个自定义 ContextLoader
,则它必须实现https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/aot/AotContextLoader.html[AotContextLoader
] 才能提供 AOT 构建时处理和 AOT 运行时执行支持。不过,请注意,Spring Framework 和 Spring Boot 提供的所有上下午装载器实现都已实现了 AotContextLoader
。
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
.
如果您实现了一个自定义 TestExecutionListener
,则它必须实现https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/aot/AotTestExecutionListener.html[AotTestExecutionListener
] 才能参与 AOT 处理。请参阅 spring-test
模块中的 SqlScriptsTestExecutionListener
以获取示例。
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.