### Question:
NMRFaultOutInterceptor extends AbstractPhaseInterceptor<NMRMessage> { public void handleMessage(NMRMessage message) throws Fault { message.put(org.apache.cxf.message.Message.RESPONSE_CODE, new Integer(500)); NSStack nsStack = new NSStack(); nsStack.push(); try { XMLStreamWriter writer = getWriter(message); Fault fault = getFault(message); NMRFault jbiFault = NMRFault.createFault(fault); nsStack.add(NMRConstants.NS_NMR_BINDING); String prefix = nsStack.getPrefix(NMRConstants.NS_NMR_BINDING); StaxUtils.writeStartElement(writer, prefix, NMRFault.NMR_FAULT_ROOT, NMRConstants.NS_NMR_BINDING); if (!jbiFault.hasDetails()) { writer.writeEmptyElement("fault"); } else { Element detail = jbiFault.getDetail(); NodeList details = detail.getChildNodes(); for (int i = 0; i < details.getLength(); i++) { if (details.item(i) instanceof Element) { StaxUtils.writeNode(details.item(i), writer, true); break; } } } writer.writeEndElement(); writer.flush(); } catch (XMLStreamException xe) { throw new Fault(new Message("XML_WRITE_EXC", BUNDLE), xe); } } NMRFaultOutInterceptor(); void handleMessage(NMRMessage message);  }### Answer:
@Test public void testDetailedFault() throws Exception { PhaseInterceptor<NMRMessage> interceptor = new NMRFaultOutInterceptor(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(baos); NMRMessage msg = new NMRMessage(new MessageImpl()); Fault f = new Fault(new Message("FAULT", (ResourceBundle) null)); f.getOrCreateDetail(); f.getDetail().appendChild(f.getDetail().getOwnerDocument().createElementNS("urn:test", "myDetails")); msg.setContent(XMLStreamWriter.class, writer); msg.setContent(Exception.class, f); interceptor.handleMessage(msg); writer.close(); Document doc = DOMUtils.readXml(new ByteArrayInputStream(baos.toByteArray())); assertEquals("urn:test", doc.getDocumentElement().getFirstChild().getNamespaceURI()); assertEquals("myDetails", doc.getDocumentElement().getFirstChild().getNodeName()); }
@Test public void testNoWriter() throws Exception { PhaseInterceptor<NMRMessage> interceptor = new NMRFaultOutInterceptor(); try { NMRMessage msg = new NMRMessage(new MessageImpl()); interceptor.handleMessage(msg); fail("Should have thrown an exception"); } catch (IllegalStateException e) { } }
@Test public void testNoFault() throws Exception { PhaseInterceptor<NMRMessage> interceptor = new NMRFaultOutInterceptor(); try { NMRMessage msg = new NMRMessage(new MessageImpl()); msg.setContent(XMLStreamWriter.class, XMLOutputFactory.newInstance().createXMLStreamWriter(new ByteArrayOutputStream())); interceptor.handleMessage(msg); fail("Should have thrown an exception"); } catch (IllegalStateException e) { } }
@Test public void testEmptyFault() throws Exception { PhaseInterceptor<NMRMessage> interceptor = new NMRFaultOutInterceptor(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(baos); NMRMessage msg = new NMRMessage(new MessageImpl()); msg.setContent(XMLStreamWriter.class, writer); msg.setContent(Exception.class, new Exception("My fault")); interceptor.handleMessage(msg); writer.close(); Document doc = DOMUtils.readXml(new ByteArrayInputStream(baos.toByteArray())); assertEquals("fault", doc.getDocumentElement().getFirstChild().getNodeName()); }