1.13. 测试原生可执行文件
以原生模式测试应用,以测试原生可执行文件的功能。使用 @QuarkusIntegrationTest
注释来构建原生可执行文件,并根据 HTTP 端点运行测试。
重要
以下示例演示了如何使用本地安装 GraalVM 或 Mandrel 测试原生可执行文件。开始之前,请考虑以下点:
- 红帽构建的 Quarkus 不支持这种情况,如 Producing a native executable 中所述。
- 您在此处测试的原生可执行文件必须与主机的操作系统和架构匹配。因此,这个过程不适用于 macOS 或 in-container 构建。
流程
打开
pom.xml
文件,并验证build
部分是否具有以下元素:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>${surefire-plugin.version}</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <systemPropertyVariables> <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </execution> </executions> </plugin>
-
Maven Failsafe 插件(
maven-failsafe-plugin
)运行集成测试,并指示生成的原生可执行文件的位置。
-
Maven Failsafe 插件(
打开
src/test/java/org/acme/GreetingResourceIT.java
文件,并验证该文件是否包含以下内容:package org.acme; import io.quarkus.test.junit.QuarkusIntegrationTest; @QuarkusIntegrationTest 1 public class GreetingResourceIT extends GreetingResourceTest { 2 // Execute the same tests but in native mode. }
运行测试:
./mvnw verify -Dnative
以下示例显示了这个命令的输出:
./mvnw verify -Dnative .... GraalVM Native Image: Generating 'getting-started-1.0.0-SNAPSHOT-runner' (executable)... ======================================================================================================================== [1/8] Initializing... (6.6s @ 0.22GB) Java version: 17.0.7+7, vendor version: Mandrel-23.1.0.0-Final Graal compiler: optimization level: 2, target machine: x86-64-v3 C compiler: gcc (redhat, x86_64, 13.2.1) Garbage collector: Serial GC (max heap size: 80% of RAM) 2 user-specific feature(s) - io.quarkus.runner.Feature: Auto-generated class by Red Hat build of Quarkus from the existing extensions - io.quarkus.runtime.graal.DisableLoggingFeature: Disables INFO logging during the analysis phase [2/8] Performing analysis... [******] (40.0s @ 2.05GB) 10,318 (86.40%) of 11,942 types reachable 15,064 (57.36%) of 26,260 fields reachable 52,128 (55.75%) of 93,501 methods reachable 3,298 types, 109 fields, and 2,698 methods registered for reflection 63 types, 68 fields, and 55 methods registered for JNI access 4 native libraries: dl, pthread, rt, z [3/8] Building universe... (5.9s @ 1.31GB) [4/8] Parsing methods... [**] (3.7s @ 2.08GB) [5/8] Inlining methods... [***] (2.0s @ 1.92GB) [6/8] Compiling methods... [******] (34.4s @ 3.25GB) [7/8] Layouting methods... [[7/8] Layouting methods... [**] (4.1s @ 1.78GB) [8/8] Creating image... [**] (4.5s @ 2.31GB) 20.93MB (48.43%) for code area: 33,233 compilation units 21.95MB (50.80%) for image heap: 285,664 objects and 8 resources 337.06kB ( 0.76%) for other data 43.20MB in total .... [INFO] [INFO] --- maven-failsafe-plugin:3.0.0-M7:integration-test (default) @ getting-started --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running org.acme.GreetingResourceIT __ ____ __ _____ ___ __ ____ ______ --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 2024-06-27 14:04:52,681 INFO [io.quarkus] (main) getting-started 1.0.0-SNAPSHOT native (powered by Quarkus 3.8.6.SP1-redhat-00002) started in 0.038s. Listening on: http://0.0.0.0:8081 2024-06-27 14:04:52,682 INFO [io.quarkus] (main) Profile prod activated. 2024-06-27 14:04:52,682 INFO [io.quarkus] (main) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.696 s - in org.acme.GreetingResourceIT [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-failsafe-plugin:3.0.0-M7:verify (default) @ getting-started ---
注意Quarkus 在自动失败原生测试前等待 60 秒启动原生镜像。您可以通过配置
quarkus.test.wait-time
系统属性来更改此持续时间。您可以使用以下命令扩展等待时间,其中 <
duration>
是等待时间(以秒为单位):./mvnw verify -Dnative -Dquarkus.test.wait-time=<duration>
注意-
默认情况下,使用
prod
配置集运行原生测试,除非在quarkus.test.native-image-profile
属性中进行了修改。
-
默认情况下,使用