Here is my Test
@Test
@WithUserDetails(USER_USERNAME)
void shouldCreateVote_whenPost() throws Exception {
mockMvc.perform(
post("/vote")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(VOTE_CREATION_ADRIANO)))
.andExpect(status().isCreated());
Mockito.verify(voteService, times(1))
.addVote(refEq(BASIC_USER_ENTITY), refEq(VOTE_CREATION_ADRIANO));
}
It fails because AuthentificationPrinciple is null
Body = {"status":400,"message":"Cannot invoke \"com..grad.restaurant.security.AuthenticatedUser.getUser()\" because \"authenticatedUser\" is null","timeStamp":1629649641723}
Here is the Controller I am testing
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@AuthenticationPrincipal AuthenticatedUser authenticatedUser,
@RequestBody @Valid VoteCreationDTO voteCreationDTO,
BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
throwExceptionIfBindingResultHasErrors(bindingResult);
}
voteService.addVote(authenticatedUser.getUser(), voteCreationDTO);
}