-
Notifications
You must be signed in to change notification settings - Fork 6k
@WithUserDetails
setupBefore
does not work with JUnit 5
#6591
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
Comments
I stumbled upon this issue, too. I would like to create a custom |
For anyone with this problem, as this is still not resolved. private const val USERNAME = "username"
private val user = User(USERNAME)
@SpringBootTest
@WithUserDetails(USERNAME)
class TestClass @Autowired constructor(
private val userRepo: UserRepository
){
@PostConstruct
fun saveUser(){
userRepo.save(user)
}
@Test
fun someTest(){
<user is available here>
}
} |
Currently, there is support for setting up a SecurityContext after @before by using TestExecutionEvent.TEST_EXECUTION. The current implementation, however, already creates the SecurityContext in @before and just does not set it yet. This leads to issues like spring-projects#6591. For the case of @WithUserDetails, the creation of the SecurityContext already looks up a user from the repository. If the user was inserted in @before, the user is not found despite using TestExecutionEvent.TEST_EXECUTION. This commit changes the creation of the SecurityContext to happen after @before if using TestExecutionEvent.TEST_EXECUTION. Closes spring-projectsgh-6591
Not specific to JUnit 5, same problem also affects vintage JUnit4 tests. |
This is now fixed via gh-6591 Closing as duplicate of the PR |
Summary
When using JUnit 5 it seems that setting the
setupBefore
parameter of@WithUserDetails
toTestExecutionEvent.TEST_EXECUTION
does not work as intended. In fact it appears that the security context is created before the execution of a@BeforeEach
method.Actual Behavior
I wrote the following test class to showcase the issue
What happens here is that the test keeps failing because when the
UserDetails
is actually loaded theUSERNAME
variable is still null.Expected Behavior
I would expect the
@BeforeEach
method to be executed before theSecurityContext
initialization therefore allowing the creation of aUserDetails
with a non null username.Configuration
Version
I am using Spring Boot Dependency
2.1.3.RELEASE
(=> Spring Security Test5.1.4.RELEASE
).Sample
The text was updated successfully, but these errors were encountered: