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

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

Closed
naihil opened this issue Feb 1, 2023 · 2 comments
Assignees
Labels
in: test An issue in spring-security-test type: bug A general bug
Milestone

Comments

@naihil
Copy link

naihil commented Feb 1, 2023

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>()
       [email protected]/java.lang.Class.getConstructor0(DynamicHub.java:3585)
       [email protected]/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?

@naihil naihil added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Feb 1, 2023
@marcusdacoregio marcusdacoregio self-assigned this Feb 2, 2023
@marcusdacoregio marcusdacoregio added in: test An issue in spring-security-test and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 2, 2023
@marcusdacoregio
Copy link
Contributor

Hi @naihil, thank you for the report.

It is indeed a bug and it has been fixed in spring-projects/spring-framework#29855, which will be available in Spring Framework 6.0.5, scheduled for Feb 15th, and Spring Security 6.0.2 scheduled for Feb 20th.

You can change the Spring Framework version to 6.0.5-SNAPSHOT to test the fix. I already did that using your sample and the test passes.

@marcusdacoregio marcusdacoregio added this to the 6.0.2 milestone Feb 3, 2023
@naihil
Copy link
Author

naihil commented Feb 3, 2023

Nice to hear that! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test An issue in spring-security-test type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants