Sub topic 12
Running tests as a native image
This chapter assumes you can build a Spring Boot project with Maven or Gradle and have compiled a native executable as covered earlier. It also assumes you know how Spring AOT fixes bean definitions at build time. Here you will run your test suite as a native image, so that failures that only appear in native mode show up before production.
Why test in native mode
A test that passes on the JVM tells you the code works with a JIT compiler and full runtime reflection. A native executable has neither. Code that reads a resource, reflects on a class or creates a proxy can pass every JVM test and still fail in the native executable because the build did not include the needed metadata.
Native testing closes that gap. The build compiles your tests, and the Spring components they use, into a native test executable. It then runs that executable with JUnit Platform.
Running the native tests with Maven
With a project that inherits from spring-boot-starter-parent, activate the nativeTest profile:
./mvnw -PnativeTest testThis command does the following:
- Runs Spring AOT processing on the main code and on the test code.
- Compiles the tests and the application classes they reach with
native-image. - Runs the resulting test executable and reports results in the usual surefire format.
On Gradle, with the org.graalvm.buildtools.native plugin applied, the equivalent task is ./gradlew nativeTest.
What a native test run checks
The native test executable uses the same closed-world analysis as your application. Anything the tests reach by reflection, resources or proxies must be known at build time. A missing hint therefore fails the test run, which is the point.
The flow looks like this:
A test that catches a missing hint
Suppose the AppConfig from the runtime hints chapter loads templates/greeting.txt from the classpath. The test below reads that resource through the ClassLoader:
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class GreetingResourceTests {
@Test
void greetingTemplateIsOnTheClasspath() {
var stream = getClass().getClassLoader()
.getResourceAsStream("templates/greeting.txt");
assertNotNull(stream);
}
}On the JVM this passes, because the file sits on the classpath. In a native test it passes only if the resource was registered, for example with hints.resources().registerPattern("templates/greeting.txt"). If you remove that line, the JVM test still passes, but ./mvnw -PnativeTest test fails with a null stream. The failure shows a gap in your hints that the JVM run hid.
Limits to know about
- Tests that use features Spring AOT does not support, such as some
@MockBeanand@SpyBeanpatterns or@DirtiesContext-heavy designs, may need changes or may not run natively. Check the Spring Boot reference for the current list of supported test features. - Test-only libraries need their own reachability metadata. Mature libraries, such as JUnit and Mockito, ship it or are covered by the GraalVM reachability metadata repository.
- Build-time conditions apply to tests too. A property that selects a bean through
@ConditionalOnPropertymust have its final value during AOT processing, not only when the test starts.
Common errors
| Symptom | Likely cause |
|---|---|
| Test passes on the JVM, fails natively with a missing class or method | Reflection use without a hint |
| Resource stream is null in native mode only | Resource pattern not registered |
| Bean missing in the native test only | Condition evaluated at build time with a different property value |
| Build runs out of memory | `native-image` needs more heap than the build machine offers |
Exercise
Your JVM tests all pass, but ./mvnw -PnativeTest test fails because a class loaded by name cannot be found. What does this indicate, and what are two ways to fix it?
Show answer
The class is reached through reflection and the native build has no metadata for it. Fix it by registering a hint, for example hints.reflection().registerType(...) in a RuntimeHintsRegistrar imported with @ImportRuntimeHints, or by running the application on the JVM with -agentlib:native-image-agent and putting the generated metadata files under META-INF/native-image.
Exercise
Why can a bean guarded by @ConditionalOnProperty be present in a JVM test but absent in a native test?
Show answer
Spring evaluates the condition during AOT processing, so the property value at build time decides whether the bean exists. A value set only when the test starts has no effect on the native test executable.