### Question:
DocumentStringUserReferenceSerializer implements UserReferenceSerializer<String> { @Override public String serialize(UserReference userReference) { String result; if (userReference == null) { result = null; } else if (SuperAdminUserReference.INSTANCE == userReference) { result = SUPERADMIN_REFERENCE_STRING; } else if (GuestUserReference.INSTANCE == userReference) { result = GUEST_REFERENCE_STRING; } else if (CurrentUserReference.INSTANCE == userReference) { UserReference resolvedUserReference = this.currentUserReferenceUserReferenceResolver.resolve(null); if (GuestUserReference.INSTANCE == resolvedUserReference) { result = GUEST_REFERENCE_STRING; } else { result = serializeInternal(resolvedUserReference); } } else { result = serializeInternal(userReference); } return result; }  @Override String serialize(UserReference userReference);  }### Answer:
@Test void serialize() { DocumentReference documentReference = new DocumentReference("wiki", "space", "user"); DocumentUserReference documentUserReference = new DocumentUserReference(documentReference, null); this.serializer.serialize(documentUserReference); verify(this.entityReferenceSerializer).serialize(documentReference); }
@Test void serializeWhenNotDocumentUserReference() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> this.serializer.serialize(mock(UserReference.class))); assertEquals("Only DocumentUserReference are handled", exception.getMessage()); }
@Test void serializeWhenNull() { assertNull(this.serializer.serialize(null)); }
@Test void serializeSuperAdminReference() { assertEquals("XWiki.superadmin", this.serializer.serialize(SuperAdminUserReference.INSTANCE)); }
@Test void serializeGuestReference() { assertEquals("XWiki.XWikiGuest", this.serializer.serialize(GuestUserReference.INSTANCE)); }
@Test void serializeCurrentUserReferenceWhenCurrentUserExists() { DocumentUserReference userReference = mock(DocumentUserReference.class); DocumentReference documentReference = new DocumentReference("wiki", "space", "user"); when(userReference.getReference()).thenReturn(documentReference); when(this.currentUserReferenceUserReferenceResolver.resolve(null)).thenReturn(userReference); when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:space.user"); assertEquals("wiki:space.user", this.serializer.serialize(CurrentUserReference.INSTANCE)); }
@Test void serializeCurrentUserReferenceWhenNoCurrentUser() { when(this.currentUserReferenceUserReferenceResolver.resolve(null)).thenReturn(GuestUserReference.INSTANCE); assertEquals("XWiki.XWikiGuest", this.serializer.serialize(CurrentUserReference.INSTANCE)); }