### Question:
JaxbUtil { public String marshal(Object jaxbObject) { if (jaxbContext == null) { logger.error("Trying to marshal with a null jaxbContext, returns null. Check your configuration, e.g. jaxb-transformers!"); return null; } try { StringWriter writer = new StringWriter(); Marshaller marshaller = jaxbContext.createMarshaller(); if (marshallProps != null) { for (Entry<String, Object> entry : marshallProps.entrySet()) { marshaller.setProperty(entry.getKey(), entry.getValue()); } } marshaller.marshal(jaxbObject, writer); String xml = writer.toString(); if (logger.isDebugEnabled()) logger.debug("marshalled jaxb object of type {}, returns xml: {}", jaxbObject.getClass().getSimpleName(), xml); return xml; } catch (JAXBException e) { throw new RuntimeException(e); } } @SuppressWarnings("rawtypes")  JaxbUtil(Class... classesToBeBound);  JaxbUtil(String contextPath); JAXBContext getContext(); void setContextPath(String contextPath); void addMarshallProperty(String name, Object value); void addUnmarshallProperty(String name, Object value); String marshal(Object jaxbObject); @SuppressWarnings({ "unchecked", "rawtypes" }) String marshal(Object jaxbObject, String namespaceURI, String localPart); @SuppressWarnings("rawtypes") Object unmarshal(Object payload);  }### Answer:
@Test public void testMarshal() { String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<logEvent xmlns=\"urn:org.soitoolkit.commons.logentry.schema:v1\">" + "<logEntry>" + "<payload>MyPayload</payload>" + "</logEntry>" + "</logEvent>"; LogEvent logEvent = new LogEvent(); LogEntryType logEntry = new LogEntryType(); logEvent.setLogEntry(logEntry); logEntry.setPayload("MyPayload"); JaxbUtil jaxbUtil = new JaxbUtil(LogEvent.class); String actualXml = jaxbUtil.marshal(logEvent); log.info(actualXml); assertEquals(expectedXml, actualXml); }
@Test public void testMarshalWithoutXmlRootElement() { String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<logEntry xmlns=\"urn:org.soitoolkit.commons.logentry.schema:v1\">" + "<payload>MyPayload</payload>" + "</logEntry>"; LogEntryType logEntry = new LogEntryType(); logEntry.setPayload("MyPayload"); JaxbUtil jaxbUtil = new JaxbUtil(LogEvent.class); String actualXml = jaxbUtil.marshal(logEntry, "urn:org.soitoolkit.commons.logentry.schema:v1", "logEntry"); log.info(actualXml); assertEquals(expectedXml, actualXml); }