Tools { public static String marshalToXml(Object obj, Class sourceClass) { Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller(); jaxb2.setContextPath(sourceClass.getPackage().getName()); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); jaxb2.marshal(obj, result); return writer.toString(); } private  Tools(); static String toString(Object value); static String joinNonEmpty(String[] array, char separator); static String marshalToXml(Object obj, Class sourceClass); @SuppressWarnings("unchecked") static String marshalToXml(T obj, QName qName); @SuppressWarnings("unchecked") static T unmarshalFromXml(String xml, Class<T> targetClass); static OffsetDateTime toUTC(OffsetDateTime localDateTime); static OffsetDateTime fromUTC(OffsetDateTime utcTime); static OffsetDateTime fromUTC(long utcMillis); static Set<String> getAllKnownPropertyNames(ConfigurableEnvironment env); static Properties getPropertiesWithPrefix(ConfigurableEnvironment env, String propertyNamePrefix); static String fm(String message, Object... values);  }
@Test public void testMarshalFromXml() { HelloRequest req = new HelloRequest(); req.setName("Peter"); String xml = Tools.marshalToXml(req, HelloRequest.class); assertThat(xml, CoreMatchers.containsString("<helloRequest>")); assertThat(xml, CoreMatchers.containsString("</helloRequest>")); assertThat(xml, CoreMatchers.containsString("<name>")); assertThat(xml, CoreMatchers.containsString("Peter")); xml = Tools.marshalToXml(req, QName.valueOf("hello")); assertThat(xml, CoreMatchers.containsString("<hello>")); assertThat(xml, CoreMatchers.containsString("</hello>")); assertThat(xml, CoreMatchers.containsString("<name>")); assertThat(xml, CoreMatchers.containsString("Peter")); }