Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing hints for SpringBootMockMvcBuilderCustomizer #35601

Closed
sbrannen opened this issue May 23, 2023 · 2 comments
Closed

Missing hints for SpringBootMockMvcBuilderCustomizer #35601

sbrannen opened this issue May 23, 2023 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@sbrannen
Copy link
Member

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)
       [email protected]/java.lang.reflect.Method.acquireMethodAccessor(Method.java:71)
       [email protected]/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);
	}

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 23, 2023
@wilkinsona
Copy link
Member

Duplicates #35564.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2023
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels May 23, 2023
@sbrannen
Copy link
Member Author

I apologize for creating a duplicate issue.

I searched the issue tracker for WebMvcTest instead of SpringBootMockMvcBuilderCustomizer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants