### Question:
DocumentUserResourceReferenceSerializer implements ResourceReferenceSerializer<UserResourceReference, ExtendedURL> { @Override public ExtendedURL serialize(UserResourceReference resourceReference) throws SerializeResourceReferenceException { UserReference userReference = resourceReference.getUserReference(); if (!(userReference instanceof DocumentUserReference)) { throw new IllegalArgumentException("The passed user resource reference is not pointing to a wiki Document"); } DocumentReference documentReference = ((DocumentUserReference) userReference).getReference(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory urlFactory = xcontext.getURLFactory(); URL url = createURL(urlFactory, documentReference, xcontext); ExtendedURL extendedURL; try { extendedURL = new ExtendedURL(url, null); } catch (CreateResourceReferenceException e) { throw new SerializeResourceReferenceException( String.format("Failed to serialize user reference [%s] into a [%s]", documentReference, ExtendedURL.class.getName()), e); } return extendedURL; }  @Override ExtendedURL serialize(UserResourceReference resourceReference);  }### Answer:
@Test void serialize() throws Exception { DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); DocumentUserReference documentUserReference = mock(DocumentUserReference.class); when(documentUserReference.getReference()).thenReturn(documentReference); UserResourceReference userResourceReference = new UserResourceReference(documentUserReference); XWikiContext xcontext = mock(XWikiContext.class); XWikiURLFactory urlFactory = mock(XWikiURLFactory.class); when(urlFactory.createURL("space", "page", xcontext)).thenReturn(new URL("https: when(xcontext.getURLFactory()).thenReturn(urlFactory); when(this.xwikiContextProvider.get()).thenReturn(xcontext); ExtendedURL extendedURL = this.serializer.serialize(userResourceReference); assertEquals("/path/user", extendedURL.serialize()); verify(this.modelContext).setCurrentEntityReference(documentReference.getWikiReference()); }
@Test void serializeWhenNotDocumentUserReference() { UserResourceReference userReference = mock(UserResourceReference.class); Throwable exception = assertThrows(RuntimeException.class, () -> this.serializer.serialize(userReference)); assertEquals("The passed user resource reference is not pointing to a wiki Document", exception.getMessage()); }