### Question:
DefaultUserManager implements UserManager { @Override public boolean exists(UserReference userReference) { boolean exists; UserReference normalizedUserReference = userReference; if (normalizedUserReference == null) { normalizedUserReference = CurrentUserReference.INSTANCE; } if (GuestUserReference.INSTANCE == normalizedUserReference || SuperAdminUserReference.INSTANCE == normalizedUserReference) { exists = false; } else { exists = resolveUserManager(normalizedUserReference).exists(normalizedUserReference); } return exists; }  @Override boolean exists(UserReference userReference);  }### Answer:
@Test void exists() throws Exception { UserManager customUserManager = mock(UserManager.class); when(customUserManager.exists(any(TestUserReference.class))).thenReturn(true); when(this.contextComponentManager.getInstance(UserManager.class, TestUserReference.class.getName())) .thenReturn(customUserManager); assertTrue(this.userManager.exists(new TestUserReference())); }
@Test void existsWhenSuperAdmin() { assertFalse(this.userManager.exists(SuperAdminUserReference.INSTANCE)); }
@Test void existsWhenGuest() { assertFalse(this.userManager.exists(GuestUserReference.INSTANCE)); }
@Test void existsWhenNull() throws Exception { UserManager currentUserManager = mock(UserManager.class); when(this.contextComponentManager.getInstance(UserManager.class, CurrentUserReference.class.getName())) .thenReturn(currentUserManager); this.userManager.exists(null); verify(currentUserManager).exists(CurrentUserReference.INSTANCE); }
@Test void existsWhenCurrentUser() throws Exception { UserManager currentUserManager = mock(UserManager.class); when(this.contextComponentManager.getInstance(UserManager.class, CurrentUserReference.class.getName())) .thenReturn(currentUserManager); this.userManager.exists(CurrentUserReference.INSTANCE); verify(currentUserManager).exists(CurrentUserReference.INSTANCE); }
@Test void existsWhenNoUserManager() throws Exception { when(this.contextComponentManager.getInstance(UserManager.class, TestUserReference.class.getName())) .thenThrow(new ComponentLookupException("error")); Throwable exception = assertThrows(RuntimeException.class, () -> { this.userManager.exists(new TestUserReference()); }); assertEquals("Failed to find user manager for role [org.xwiki.user.UserManager] and hint " + "[org.xwiki.user.internal.DefaultUserManagerTest$TestUserReference]", exception.getMessage()); assertEquals("ComponentLookupException: error", ExceptionUtils.getRootCauseMessage(exception)); }