### Question:
SecurityChecker { public static void checkUser(final String userId) { checkAnyUser(); final String contextUserId = SecurityContextHolder.get().userId().get(); if (!contextUserId.equals(userId)) { throw new SecurityConstraintViolationException("Access denied for principal: " + contextUserId); } }  static void checkUser(final String userId); static void checkTeam(final String teamId); static void checkAnyUser(); static void checkAnyTeam();  }### Answer:
@Test public void shouldPassUserCheck_withMatchingUserIdInSecurityContext() { final String userId = randomId(); when(mockSecurityContext.userId()).thenReturn(Optional.of(userId)); Exception thrownException = null; try { SecurityChecker.checkUser(userId); } catch (final Exception e) { thrownException = e; } assertNull(thrownException); }
@Test public void shouldNotPassUserCheck_withNoUserIdInSecurityContext() { final String userId = randomId(); when(mockSecurityContext.userId()).thenReturn(Optional.empty()); CredentialsMissingException thrownException = null; try { SecurityChecker.checkUser(userId); } catch (final CredentialsMissingException e) { thrownException = e; } assertNotNull(thrownException); }
@Test public void shouldNotPassUserCheck_withDifferentUserIdInSecurityContext() { final String userId = randomId(); when(mockSecurityContext.userId()).thenReturn(Optional.of(randomId())); SecurityConstraintViolationException thrownException = null; try { SecurityChecker.checkUser(userId); } catch (final SecurityConstraintViolationException e) { thrownException = e; } assertNotNull(thrownException); }