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?
Comment From: marcusdacoregio
Hi @naihil, thank you for the report.
It is indeed a bug and it has been fixed in https://github.com/spring-projects/spring-framework/issues/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.
Comment From: naihil
Nice to hear that! Thanks!