### Question:
WisdomServiceVisitor extends AnnotationVisitor { public void visitEnd() { String classname = workbench.getType().getClassName(); component.addAttribute(new Attribute("classname", classname)); component.addElement(ElementHelper.getProvidesElement(specifications)); if (workbench.getRoot() == null) { workbench.setRoot(component); workbench.setInstance(ElementHelper.declareInstance(workbench)); } else { reporter.error("Multiple 'component type' annotations on the class '{%s}'.", classname); reporter.warn("@Service is ignored."); } } WisdomServiceVisitor(ComponentWorkbench workbench, Reporter reporter); @Override AnnotationVisitor visitArray(String s); void visitEnd();  }### Answer:
@Test public void testOnlyService() { Reporter reporter = mock(Reporter.class); ComponentWorkbench workbench = mock(ComponentWorkbench.class); when(workbench.getType()).thenReturn(Type.getType(MyComponent.class)); when(workbench.getClassNode()).thenReturn(new ClassNode()); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { root = (Element) invocation.getArguments()[0]; return null; } }).when(workbench).setRoot(any(Element.class)); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { instance = (Element) invocation.getArguments()[0]; return null; } }).when(workbench).setInstance(any(Element.class)); WisdomServiceVisitor visitor = new WisdomServiceVisitor(workbench, reporter); visitor.visitEnd(); assertThat(root).isNotNull(); assertThat(root.getName()).isEqualTo("component"); assertThat(root.getAttribute("classname")).isEqualTo(MyComponent.class.getName()); assertThat(root.getElements("provides")).hasSize(1); assertThat(instance).isNotNull(); assertThat(instance.getAttribute("component")).isEqualTo(MyComponent.class.getName()); }
@Test public void testWhenAnotherRootIsAlreadyDeclared() { Reporter reporter = mock(Reporter.class); ComponentWorkbench workbench = mock(ComponentWorkbench.class); when(workbench.getRoot()).thenReturn(new Element("another", "root")); when(workbench.getType()).thenReturn(Type.getType(MyComponent.class)); when(workbench.getClassNode()).thenReturn(new ClassNode()); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { root = (Element) invocation.getArguments()[0]; return null; } }).when(workbench).setRoot(any(Element.class)); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { instance = (Element) invocation.getArguments()[0]; return null; } }).when(workbench).setInstance(any(Element.class)); WisdomServiceVisitor visitor = new WisdomServiceVisitor(workbench, reporter); visitor.visitEnd(); assertThat(root).isNull(); assertThat(instance).isNull(); }