Testing GraalVM Native Images
在编写本机映像应用程序时,我们建议你在可能的情况下继续使用 JVM 开发大多数单元和集成测试。这将有助于缩短开发人员的构建时间,并允许你使用现有的 IDE 集成。在 JVM 上进行广泛的测试后,你就可以重点关注本机映像测试可能有所不同的领域。
When writing native image applications, we recommend that you continue to use the JVM whenever possible to develop the majority of your unit and integration tests. This will help keep developer build times down and allow you to use existing IDE integrations. With broad test coverage on the JVM, you can then focus native image testing on the areas that are likely to be different.
对于本机映像测试,你通常需要确保以下方面:
For native image testing, you’re generally looking to ensure that the following aspects work:
-
The Spring AOT engine is able to process your application, and it will run in an AOT-processed mode.
-
GraalVM has enough hints to ensure that a valid native image can be produced.
Testing Ahead-of-time Processing With the JVM
当 Spring Boot 应用程序运行时,它会尝试检测它是否作为本机映像运行。如果它作为本机映像运行,它将使用 Spring AOT 引擎在构建时生成代码初始化应用程序。
When a Spring Boot application runs, it attempts to detect if it is running as a native image. If it is running as a native image, it will initialize the application using the code that was generated during at build-time by the Spring AOT engine.
如果应用程序在常规 JVM 上运行,那么会忽略任何 AOT 生成的代码。
If the application is running on a regular JVM, then any AOT generated code is ignored.
由于 native-image
编译阶段可能需要一段时间才能完成,因此有时在 JVM 上运行应用程序但使用 AOT 生成的初始化代码很有用。这样做有助于你快速验证 AOT 生成的代码中没有错误,并且当最终将应用程序转换为本机映像后也没有遗漏任何内容。
Since the native-image
compilation phase can take a while to complete, it’s sometimes useful to run your application on the JVM but have it use the AOT generated initialization code.
Doing so helps you to quickly validate that there are no errors in the AOT generated code and nothing is missing when your application is eventually converted to a native image.
若要在 JVM 上运行 Spring Boot 应用程序并使用 AOT 生成的代码,你可以将 spring.aot.enabled
系统属性设置为 true
。
To run a Spring Boot application on the JVM and have it use AOT generated code you can set the spring.aot.enabled
system property to true
.
例如:
For example:
$ java -Dspring.aot.enabled=true -jar myapplication.jar
你需要确保你正在测试的 jar 包含 AOT 生成的代码。对于 Maven,这意味着你应该使用 |
You need to ensure that the jar you are testing includes AOT generated code.
For Maven, this means that you should build with |
如果你的应用程序以 spring.aot.enabled
属性设置为 true
启动,那么你更有信心在将其转换为本机映像后它能够正常工作。
If your application starts with the spring.aot.enabled
property set to true
, then you have higher confidence that it will work when converted to a native image.
你还可以考虑对正在运行的应用程序运行集成测试。例如,你可以使用 Spring WebClient
调用你的应用程序 REST 端点。或者,你可以考虑使用 Selenium 等项目来检查你的应用程序的 HTML 响应。
You can also consider running integration tests against the running application.
For example, you could use the Spring WebClient
to call your application REST endpoints.
Or you might consider using a project like Selenium to check your application’s HTML responses.
Testing With Native Build Tools
GraalVM Native Build Tools 包括在 native image 中运行测试的能力。在你希望深度测试应用程序的内部功能在 GraalVM native image 中的工作方式时,这会很有用。
GraalVM Native Build Tools includes the ability to run tests inside a native image. This can be helpful when you want to deeply test that the internals of your application work in a GraalVM native image.
生成包含要运行测试的 native image 可能是一项耗时的操作,因此大多数开发人员可能会更喜欢在本地使用 JVM。不过,它们作为 CI 管道的组成部分可能非常有用。例如,你可能选择每天运行一次本机测试。
Generating the native image that contains the tests to run can be a time-consuming operation, so most developers will probably prefer to use the JVM locally. They can, however, be very useful as part of a CI pipeline. For example, you might choose to run native tests once a day.
Spring Framework 包括针对运行测试的预处理支持。所有常规的 Spring 测试功能都适用于本机映像测试。例如,你可以继续使用 @SpringBootTest
注释。你还可以使用 Spring Boot test slices 来仅测试应用程序的特定部分。
Spring Framework includes ahead-of-time support for running tests.
All the usual Spring testing features work with native image tests.
For example, you can continue to use the @SpringBootTest
annotation.
You can also use Spring Boot test slices to test only specific parts of your application.
Spring Framework 的本机测试支持的工作方式如下:
Spring Framework’s native testing support works in the following way:
-
Tests are analyzed in order to discover any
ApplicationContext
instances that will be required. -
Ahead-of-time processing is applied to each of these application contexts and assets are generated.
-
A native image is created, with the generated assets being processed by GraalVM.
-
The native image also includes the JUnit
TestEngine
configured with a list of the discovered tests. -
The native image is started, triggering the engine which will run each test and report results.
Using Maven
要使用 Maven 运行本机测试,请确保 pom.xml`文件使用 `spring-boot-starter-parent
。你应该有一个与此类似的 `<parent>`部分:
To run native tests using Maven, ensure that your pom.xml
file uses the spring-boot-starter-parent
.
You should have a <parent>
section that looks like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{version-spring-boot}</version>
</parent>
`spring-boot-starter-parent`声明了一个 `nativeTest`配置文件,用于配置运行本机测试所需的执行操作。你可以使用命令行上的 `-P`标记激活配置文件。
The spring-boot-starter-parent
declares a nativeTest
profile that configures the executions that are needed to run the native tests.
You can activate profiles using the -P
flag on the command line.
如果不希望使用 |
If you don’t want to use |
要构建映像并运行测试,请使用已激活 `nativeTest`配置文件的 `test`目标:
To build the image and run the tests, use the test
goal with the nativeTest
profile active:
$ mvn -PnativeTest test
Using Gradle
当应用了 GraalVM 本机映像插件时,Spring Boot Gradle 插件会自动配置 AOT 测试任务。你应该检查 Gradle 构建是否包含一个包括 `org.graalvm.buildtools.native`的 `plugins`块。
The Spring Boot Gradle plugin automatically configures AOT test tasks when the GraalVM Native Image plugin is applied.
You should check that your Gradle build contains a plugins
block that includes org.graalvm.buildtools.native
.
要使用 Gradle 运行本机测试,可以使用 `nativeTest`任务:
To run native tests using Gradle you can use the nativeTest
task:
$ gradle nativeTest