SecurityContextHolder is not populated in @BeforeAll/PostConstruct
within @WithUserDetails
#15902
Labels
@BeforeAll/PostConstruct
within @WithUserDetails
#15902
Describe the bug
I'm not sure if this is the intended behavior where
SecurityContextHolder
is not populated or accessible within@BeforeAll/PostConstruct
. I searched the repository and found #6591 is quite close to what I experience/encounter.This is reproduced using
Spring Boot 3.3.4
but I encountered it in my project which is usingSpring Boot 3.2.5
, withJava 21
andJUnit 5
To Reproduce
This is the code snippet to reproduce
Expected behavior
I would expect that the authentication object is available within
@BeforeAll
andPostConstruct
method.Extra Note
I discovered this as my current test is failing where there's a method call to insert certain data in
@BeforeAll
which usesSpEL
within the repository methodSo the stack trace looks like the following...
So after some tracing, I found out that it was because the authentication object was null when called in
@BeforeAll
, and thus, causing the test to fail.I'm also using custom
@WithSecurityContext
to provide my own@WithMockXXXUser
annotation if that matters.Let me know if more information is required.
Thanks!
The text was updated successfully, but these errors were encountered: