In JUnit tests with Spring MockMVC, there are two methods for authenticating as a Spring Security user: @WithMockUser creates a dummy user with the provided credent
Unfortunately, you can't do easily @WithUserDetails with @Before, because Spring @WithUserDetails annotation will invoke Spring security context test listener before running setUp method with @Before.
Here is https://stackoverflow.com/a/38282258/1814524 a little trick and answer to your question.