RewardConfirmationMarshaller implements Marshaller { @Override public void marshal(Object graph, Result result) throws XmlMappingException, IOException { RewardConfirmation rewardConfirmation = (RewardConfirmation) graph; Assert.hasText(rewardConfirmation.getDiningTransactionId(), "'diningTransactionId' is missing from reward confirmation"); Document doc; try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } catch (Throwable e) { throw new MarshallingFailureException("Can't create new Document", e); } Element elConfirmation = (Element) doc.appendChild(doc.createElement("reward-confirmation")); elConfirmation.setAttribute("dining-transaction-id", rewardConfirmation.getDiningTransactionId()); if (result instanceof DOMResult) { ((DOMResult) result).setNode(doc); } else { throw new IllegalArgumentException("Got instance of " + result.getClass().getSimpleName() + " which is not supported"); } }  @Override boolean supports(Class<?> clazz); @Override void marshal(Object graph, Result result);  }
@Test public void marshallDOM() throws Exception { RewardConfirmation rewardConfirmation = mock(RewardConfirmation.class); when(rewardConfirmation.getDiningTransactionId()).thenReturn("w00t"); DOMResult result = new DOMResult(); marshaller.marshal(rewardConfirmation, result); DOMSource source = new DOMSource(result.getNode()); assertThat(xpathTemplate.evaluateAsString( "/reward-confirmation/@dining-transaction-id", source), is("w00t")); }