Class TestSecurityContextHolder

java.lang.Object
com.codename1.backend.test.TestSecurityContextHolder

public final class TestSecurityContextHolder extends Object

Who the calling thread's tests are running as. WithMockUser sets it for a test; a test that needs to change it part-way does so here.

TestSecurityContextHolder.setAuthentication(
        UsernamePasswordAuthenticationToken.authenticated("ada", null, authorities));

While one is set, every request the thread sends through MockMvc is from that authentication, and SecurityContextHolder on the thread holds it.

  • Method Details

    • getContext

      public static SecurityContext getContext()
      The context in place, or an empty one.
    • setContext

      public static void setContext(SecurityContext context)
      Puts context in place for this thread's tests.
    • setAuthentication

      public static void setAuthentication(Authentication authentication)
      Puts authentication in place for this thread's tests.
    • clearContext

      public static void clearContext()
      Removes what was put in place: the thread's tests run as nobody again.