### Question:
SecurityContextHolderAssertionProvider implements AssertionProvider { @Nonnull @Override public Assertion getAssertion() { return ((CasAuthenticationToken) Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()) .filter(a -> a instanceof CasAuthenticationToken) .orElseThrow(() -> new IllegalStateException( String.format("Unable to provide an %s with null or non %s authentication", Assertion.class.getSimpleName(), CasAuthenticationToken.class.getCanonicalName())))) .getAssertion(); }  @Nonnull @Override Assertion getAssertion();  }### Answer:
@Test(expected = IllegalStateException.class) public void getAssertion_NullAuthentication_IllegalStateException() { SecurityContextHolder.getContext().setAuthentication(null); AssertionProvider provider = new SecurityContextHolderAssertionProvider(); provider.getAssertion(); }
@Test(expected = IllegalStateException.class) public void getAssertion_UnwantedAuthenticationType_IllegalStateException() { Authentication authentication = new UsernamePasswordAuthenticationToken("casuser", "Mellon"); SecurityContextHolder.getContext().setAuthentication(authentication); AssertionProvider provider = new SecurityContextHolderAssertionProvider(); provider.getAssertion(); }
@Test public void getAssertion_ValidSecurityContextHolder_Assertion() { String user = "casuser"; String ticket = "ST-21-c1gk6jBcfYnatLbNExfx-0623277bc36a"; Set<SimpleGrantedAuthority> roles = Collections.singleton(new SimpleGrantedAuthority("MEMBER")); CasAuthenticationToken authentication = new CasAuthenticationToken(UUID.randomUUID().toString(), user, ticket, roles, new User(user, ticket, roles), new AssertionImpl(user)); SecurityContextHolder.getContext().setAuthentication(authentication); AssertionProvider provider = new SecurityContextHolderAssertionProvider(); assertThat(provider.getAssertion()).isNotNull(); assertThat(provider.getAssertion().getPrincipal().getName()).isEqualTo(user); }