Closed bwgjoseph closed 2 weeks ago
@bwgjoseph, thanks for reaching out. This is by design. Given that it is a method-scoped annotation, its effects are only applied in the context of that method. @BeforeEach
works because it also is a method-scoped annotation.
I discovered this as my current test is failing where there's a method call to insert certain data in @BeforeAll which uses SpEL within the repository method
This is helpful, thank you. The reason Spring Security doesn't support this approach is that each test can have a different @WithMockXXX
, meaning that each method can have a completely different user. Given that, it would be a bit smelly in the general case to execute a preparatory step using one SecurityContext
and then use a different SecurityContext
during method execution.
As a quick example for clarity, consider if your test had two test methods like so:
@BeforeAll
void addDatabaseRecords() {
this.orders.addOrderForLoggedInUser(order);
}
@WithMockUser(username="hanna")
void testWithHanna() {
// ...
}
@WIthMockUser(username="claude")
void testWithClaude() {
// ...
}
In this case, adding the record in @BeforeAll
couldn't be correct as there is no way to know which user to use since none of the methods have been invoked yet.
As it is, it's likely more correct to add the records you need using @BeforeEach
.
I hope that clarifies. If not, please consider posting to SO, adding that link here, and I and others would be happy to provide more support.
Thank you, this is indeed helpful with the explanation provided. Appreciate it!
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 https://github.com/spring-projects/spring-security/issues/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!