Authenticating in the @WebMvcTest
Simulating user login
UserController has all its methods secured, we need a way to simulate a user logging in so that we can do some actual validation on the controller’s logic.
The Spring Security test has some helper annotations like
@WithUserDetails to help us. In our production security configuration, we use
As we don’t want to involve a database, we can create a
StubUserDetailsService alternative for the automated test by:
UserDetailsServicelike our production
Keeping the users on a
Adding two users in the constructor so they are available for the tests.
usersmap to search for a matching user.