Last active
February 21, 2022 10:40
-
-
Save BjoernKW/0b9cb56dd3884a8bbbebedb6cc77718f to your computer and use it in GitHub Desktop.
Factory for minimal JUnit test setup with security context and a mocked user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bjoernkw.testing; | |
import org.springframework.security.authentication.TestingAuthenticationToken; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
public class SecurityContextFactory { | |
public static void createSecurityContext(String username) { | |
SecurityContextHolder.setContext( | |
SecurityContextHolder.createEmptyContext() | |
); | |
SecurityContextHolder.getContext().setAuthentication( | |
new TestingAuthenticationToken(username, null) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment