I reproduced this with Spring Boot 3.0.x and Maven as well as with 3.1 and Gradle.

  • create new project on start.spring.io
  • select web support (and native for 3.1)
  • modify the generated test class as below
  • run tests in native image
@WebMvcTest
class WebmvctestApplicationTests {

    @Test
    void test() {
    }

}

Test fails in native image with:

Failures (1):
  JUnit Jupiter:WebmvctestApplicationTests:test()
    MethodSource [className = 'com.example.webmvctest.WebmvctestApplicationTests', methodName = 'test', methodParameterTypes = '']
    => java.lang.IllegalStateException: Failed to load ApplicationContext for [AotMergedContextConfiguration@72deebdd testClass = com.example.webmvctest.WebmvctestApplicationTests, contextInitializerClass = com.example.webmvctest.WebmvctestApplicationTests__TestContext001_ApplicationContextInitializer, original = [WebMergedContextConfiguration@3b8fd140 testClass = com.example.webmvctest.WebmvctestApplicationTests, locations = [], classes = [com.example.webmvctest.WebmvctestApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@462b4ac8, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@7320321e, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1720688a, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@66c92293, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@d68eb8dd], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]]
       org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:142)
       org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
       org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependenciesInAotMode(DependencyInjectionTestExecutionListener.java:148)
       org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:94)
       org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241)
       [...]
     Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mockMvcBuilder': Unsatisfied dependency expressed through method 'mockMvcBuilder' parameter 0: Error creating bean with name 'springBootMockMvcBuilderCustomizer': Runtime reflection is not supported for public void org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.setAddFilters(boolean)
       org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArgument(BeanInstanceSupplier.java:315)
       org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArguments(BeanInstanceSupplier.java:258)
       org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:198)
       org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214)
       [...]
     Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootMockMvcBuilderCustomizer': Runtime reflection is not supported for public void org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.setAddFilters(boolean)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:605)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
       org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
       org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
       org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
       [...]
     Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: Runtime reflection is not supported for public void org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.setAddFilters(boolean)
       org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:89)
       java.base@17.0.5/java.lang.reflect.Method.acquireMethodAccessor(Method.java:71)
       java.base@17.0.5/java.lang.reflect.Method.invoke(Method.java:566)
       org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.setValue(JavaBeanBinder.java:397)
       org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:104)
       [...]

Annotating the test class with @ImportRuntimeHints(SpringBootMockMvcBuilderCustomizerHints.class) (implementation below) fixes the problem.

class SpringBootMockMvcBuilderCustomizerHints implements RuntimeHintsRegistrar {

    @Override
    public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
        hints.reflection().registerType(SpringBootMockMvcBuilderCustomizer.class,
            MemberCategory.INVOKE_DECLARED_METHODS);
    }

}

Comment From: wilkinsona

Duplicates https://github.com/spring-projects/spring-boot/issues/35564.

Comment From: sbrannen

I apologize for creating a duplicate issue.

I searched the issue tracker for WebMvcTest instead of SpringBootMockMvcBuilderCustomizer.