Skip to content

gradlew nativeTest fails with Failed to instantiate [org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory]: No default constructor found #12614

@naihil

Description

@naihil

Describe the bug
Spring Boot 3.0.2 + web + security + graalvm 22.3.0
Test with annotation @WithUserDetails fails with exception when run ./gradlew nativeTest, but is OK in JVM test run

  JUnit Jupiter:IndexControllerTest:getStatus()
    MethodSource [className = 'com.example.nativesecurity.IndexControllerTest', methodName = 'getStatus', methodParameterTypes = '']
    => java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory': Failed to instantiate [org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory]: No default constructor found
       org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createFactory(WithSecurityContextTestExecutionListener.java:176)
       org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createTestSecurityContext(WithSecurityContextTestExecutionListener.java:134)
       org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createTestSecurityContext(WithSecurityContextTestExecutionListener.java:113)
       org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.beforeTestMethod(WithSecurityContextTestExecutionListener.java:81)
       org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:288)
       [...]
     Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory': Failed to instantiate [org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory]: No default constructor found
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1306)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1198)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:321)
       [...]
     Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory]: No default constructor found
       org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
       org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1300)
       [...]
     Caused by: java.lang.NoSuchMethodException: org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.<init>()
       java.base@17.0.5/java.lang.Class.getConstructor0(DynamicHub.java:3585)
       java.base@17.0.5/java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754)
       org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:79)
       [...]

I tried to add reflect-config.json with following content and this not helped:

[
  {
    "name" : "org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true
  }
]

To Reproduce
Checkout sample and ./gradlew nativeTest

Expected behavior
Test should run fine

Sample
https://github.com/naihil/native-security

Maybe there is some other workarounds?

Metadata

Metadata

Labels

in: testAn issue in spring-security-testtype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions