### Question:
DocumentUserManager implements UserManager { @Override public boolean exists(UserReference userReference) { boolean result; XWikiContext xcontext = this.xwikiContextProvider.get(); XWiki xwiki = xcontext.getWiki(); DocumentReference userDocumentReference = ((DocumentUserReference) userReference).getReference(); try { XWikiDocument document = xwiki.getDocument(userDocumentReference, xcontext); result = !document.isNew() && document.getXObject(USERS_XCLASS_REFERENCE) != null; } catch (Exception e) { this.logger.warn(String.format("Failed to check if document [%s] holds an XWiki user or not. " + "Considering it's not the case. Root error: [%s]", userDocumentReference, ExceptionUtils.getRootCauseMessage(e))); result = false; } return result; }  @Override boolean exists(UserReference userReference);  }### Answer:
@Test void exists() throws Exception { DocumentReference reference = new DocumentReference("wiki", "space", "user"); XWiki xwiki = mock(XWiki.class); XWikiContext xcontext = mock(XWikiContext.class); when(xcontext.getWiki()).thenReturn(xwiki); when(this.contextProvider.get()).thenReturn(xcontext); XWikiDocument document = mock(XWikiDocument.class); when(xwiki.getDocument(reference, xcontext)).thenReturn(document); when(document.isNew()).thenReturn(false); when(document.getXObject(new EntityReference("XWikiUsers", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))).thenReturn(mock(BaseObject.class)); assertTrue(this.userManager.exists(new DocumentUserReference(reference, null))); }
@Test void existsWhenDocumentIsNotAUser() throws Exception { DocumentReference reference = new DocumentReference("wiki", "space", "user"); XWiki xwiki = mock(XWiki.class); XWikiContext xcontext = mock(XWikiContext.class); when(xcontext.getWiki()).thenReturn(xwiki); when(this.contextProvider.get()).thenReturn(xcontext); XWikiDocument document = mock(XWikiDocument.class); when(document.isNew()).thenReturn(false); when(xwiki.getDocument(reference, xcontext)).thenReturn(document); when(document.getXObject(new EntityReference("XWikiUsers", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))).thenReturn(null); assertFalse(this.userManager.exists(new DocumentUserReference(reference, null))); }
@Test void existsWhenNoDocument() throws Exception { DocumentReference reference = new DocumentReference("wiki", "space", "user"); XWiki xwiki = mock(XWiki.class); XWikiContext xcontext = mock(XWikiContext.class); when(xcontext.getWiki()).thenReturn(xwiki); when(this.contextProvider.get()).thenReturn(xcontext); XWikiDocument document = mock(XWikiDocument.class); when(document.isNew()).thenReturn(true); when(xwiki.getDocument(reference, xcontext)).thenReturn(document); assertFalse(this.userManager.exists(new DocumentUserReference(reference, null))); }
@Test void existsWhenError() throws Exception { DocumentReference reference = new DocumentReference("wiki", "space", "user"); XWiki xwiki = mock(XWiki.class); XWikiContext xcontext = mock(XWikiContext.class); when(xcontext.getWiki()).thenReturn(xwiki); when(this.contextProvider.get()).thenReturn(xcontext); when(xwiki.getDocument(reference, xcontext)).thenThrow(new XWikiException(0, 0, "error")); assertFalse(this.userManager.exists(new DocumentUserReference(reference, null))); assertEquals("Failed to check if document [wiki:space.user] holds an XWiki user or not. Considering it's not " + "the case. Root error: [XWikiException: Error number 0 in 0: error]", logCapture.getMessage(0)); }