### Question:
WikiDescriptorListener implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument document = (XWikiDocument) source; if (event instanceof DocumentDeletedEvent || event instanceof DocumentUpdatedEvent) { removeExistingDescriptor(document.getOriginalDocument()); } List<BaseObject> serverClassObjects = document.getXObjects(SERVER_CLASS); if (serverClassObjects != null && !serverClassObjects.isEmpty()) { DefaultWikiDescriptor descriptor = this.builder.buildDescriptorObject(serverClassObjects, document); if (descriptor != null) { this.cache.add(descriptor); this.cache.setWikiIds(null); } } }  @Override String getName(); @Override List<Event> getEvents(); @Override void onEvent(Event event, Object source, Object data);  }### Answer:
@Test public void onDocumentDeletedEvent() throws Exception { XWikiDocument document = mock(XWikiDocument.class); XWikiDocument originalDocument = mock(XWikiDocument.class); when(document.getOriginalDocument()).thenReturn(originalDocument); Event event = new DocumentDeletedEvent(); List<BaseObject> objects = new ArrayList<>(); BaseObject object = mock(BaseObject.class); objects.add(object); when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects); DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA"); when(originalDocument.getDocumentReference()).thenReturn(documentReference); when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia"); DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias"); when(cache.getFromId("subwikia")).thenReturn(descriptor); mocker.getComponentUnderTest().onEvent(event, document, null); verify(cache).remove(descriptor.getId(), descriptor.getAliases()); verify(cache, never()).add(any(DefaultWikiDescriptor.class)); }
@Test public void onDocumentUpdatedEvent() throws Exception { XWikiDocument document = mock(XWikiDocument.class); XWikiDocument originalDocument = mock(XWikiDocument.class); when(document.getOriginalDocument()).thenReturn(originalDocument); Event event = new DocumentUpdatedEvent(); List<BaseObject> objects = new ArrayList<>(); BaseObject object = mock(BaseObject.class); objects.add(object); when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects); DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA"); when(originalDocument.getDocumentReference()).thenReturn(documentReference); when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia"); DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias"); when(cache.getFromId("subwikia")).thenReturn(descriptor); List<BaseObject> newObjects = new ArrayList<>(); BaseObject newObject = mock(BaseObject.class); newObjects.add(newObject); when(document.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(newObjects); DefaultWikiDescriptor newDescriptor = new DefaultWikiDescriptor("subwikia", "newAlias"); when(builder.buildDescriptorObject(newObjects, document)).thenReturn(newDescriptor); mocker.getComponentUnderTest().onEvent(event, document, null); verify(cache).remove(descriptor.getId(), descriptor.getAliases()); verify(cache).add(newDescriptor); }