Question:
I have the following code in my controller:
<code>@MessageMapping(MESSAGE_MAPPING)
public void sendVote(final RequestVoteDto vote, final Authentication authentication) {
final UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
voteService.createVote(vote, userDetails.getUsername());
}
</code>
<code>@MessageMapping(MESSAGE_MAPPING)
public void sendVote(final RequestVoteDto vote, final Authentication authentication) {
final UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
voteService.createVote(vote, userDetails.getUsername());
}
</code>
@MessageMapping(MESSAGE_MAPPING)
public void sendVote(final RequestVoteDto vote, final Authentication authentication) {
final UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
voteService.createVote(vote, userDetails.getUsername());
}
And this is the test I’m using:
<code>@Test
@WithMockUser(username = "userTest")
@DisplayName("Send vote should be accessible for authorized user")
void testSendVote_shouldBeAccessibleForAuthorizedUser() throws Exception {
// test code here
}
</code>
<code>@Test
@WithMockUser(username = "userTest")
@DisplayName("Send vote should be accessible for authorized user")
void testSendVote_shouldBeAccessibleForAuthorizedUser() throws Exception {
// test code here
}
</code>
@Test
@WithMockUser(username = "userTest")
@DisplayName("Send vote should be accessible for authorized user")
void testSendVote_shouldBeAccessibleForAuthorizedUser() throws Exception {
// test code here
}
However, the Authentication object in the controller is always null, so it seems like @WithMockUser(username = "userTest")
is not working as expected.
Why is this happening, and how can I fix it?