Skip to content

Instantly share code, notes, and snippets.

@BjoernKW
Last active February 21, 2022 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BjoernKW/59a2c8dd4edbe8f79510c83fb5211449 to your computer and use it in GitHub Desktop.
Save BjoernKW/59a2c8dd4edbe8f79510c83fb5211449 to your computer and use it in GitHub Desktop.
Minimal JUnit test setup with security context and a mocked user
@Test
void testMethod() throws Exception {
SecurityContextHolder.setContext(
SecurityContextHolder.createEmptyContext()
);
SecurityContextHolder.getContext().setAuthentication(
new TestingAuthenticationToken("TEST_USER", null)
);
}
@BjoernKW
Copy link
Author

BjoernKW commented Feb 15, 2022

Also see https://gist.github.com/BjoernKW/0b9cb56dd3884a8bbbebedb6cc77718f and this code snippet using that factory:

    private static final String TEST_USER = "TEST_USER";

    @Test
    void recipientListEntryReceivedEvent() throws Exception {
        SecurityContextFactory.createSecurityContext(TEST_USER);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment