Skip to content

Set properties for principal in @WithMockAuthentication #167

Answered by ch4mpy
ancavar asked this question in Q&A
Discussion options

You must be logged in to vote

@ancavar I had missed this question, sorry.

Unfortunately, Authentication#principal is an Object, so, there is no easy way I know to configure it with test annotations.

All you can do is:

  • use another annotation (like @WithMockUser, @WithJwt, @WithOpaqueToken, @WithOAuth2Login or @WithOidcLogin)
  • stick with @WithMockAuthentication and hand-configure the Authentication mock with something like that:
@Test
@WithMockAuthentication(authType = UsernamePasswordAuthenticationToken.class, principalType = UserDetails.class, authorities = "ROLE_AUTHORIZED_PERSONNEL", name = "Ch4mp")
void givenUserIsAuthenticatedWithMockedAuthenticationAndGrantedWithAuthorizedPersonnel_whenGetSecret_thenReturnsSecret(…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ch4mpy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants