Chapter 4. Testing the native executable

Test the application running in the native mode to test the functionality of the native executable. Use @NativeImageTest annotation to build the native executable and run test against the http endpoints.

Procedure

  1. Open the pom.xml file and verify that the native profile contains the following elements:

    <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>

    The failsafe-maven-plugin runs integration test and indicates the location of the produced native executable.

  2. Open the src/test/java/org/acme/quickstart/NativeGreetingResourceIT.java file and verify that it includes the following content:

    package org.acme.quickstart;
    
    
    import io.quarkus.test.junit.NativeImageTest;
    
    @NativeImageTest 1
    public class NativeGreetingResourceIT extends GreetingResourceTest { 2
    
        // Run the same tests
    
    }
    1
    Use another test runner that starts the application from the native file before the tests. The executable is retrieved using the native.image.path system property configured in the Failsafe Maven Plugin.
    2
    This example extends the GreetingResourceTest, but you can also create a new test.
  3. Run the test:

    ./mvnw verify -Pnative

    The following example shows the output of this command:

    ./mvnw verify -Pnative
    ...
    [getting-started-1.0-SNAPSHOT-runner:18820]     universe:     587.26 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      (parse):   2,247.59 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]     (inline):   1,985.70 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]    (compile):  14,922.77 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      compile:  20,361.28 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]        image:   2,228.30 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]        write:     364.35 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      [total]:  52,777.76 ms
    [INFO]
    [INFO] --- maven-failsafe-plugin:2.22.1:integration-test (default) @ getting-started ---
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running org.acme.quickstart.NativeGreetingResourceIT
    Executing [/data/home/gsmet/git/quarkus-quickstarts/getting-started/target/getting-started-1.0-SNAPSHOT-runner, -Dquarkus.http.port=8081, -Dtest.url=http://localhost:8081, -Dquarkus.log.file.path=build/quarkus.log]
    2019-04-15 11:33:20,348 INFO  [io.quarkus] (main) Quarkus 999-SNAPSHOT started in 0.002s. Listening on: http://[::]:8081
    2019-04-15 11:33:20,348 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
    [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.387 s - in org.acme.quickstart.NativeGreetingResourceIT
    ...
    Note

    Quarkus waits for 60 seconds for the native image to start before automatically failing the native tests. You can change this duration using the quarkus.test.native-image-wait-time system property.

    You can extend the wait time using the following command where <duration> is the wait time in seconds:

    ./mvnw verify -Pnative -Dquarkus.test.native-image-wait-time=<duration>

4.1. Excluding tests when running as a native executable

When you run tests against your native application, you can only interact with its HTTP endpoints. Tests do not run natively, therefore they cannot link against your application’s code like they can when running on the JVM.

You can share your test class between JVM and native executions and exclude certain tests with the @DisabledOnNativeImage annotation to run them only on the JVM.

4.2. Testing an existing native executable

You can test against the existing executable build. This allows you to run multiple sets of tests in stages on the binary after it has been build.

Procedure

  • Run a test against an already built native executable:

    ./mvnw test-compile failsafe:integration-test

    This command runs the test against the existing native image using Failsafe Maven Plugin.

  • Alternatively, you can specify the path to the native executable with the following command where <path> is the native image path:

    ./mvnw test-compile failsafe:integration-test -Dnative.image.path=<path>