public final class JaxbConverter extends Object
The object has to be annotated with JAXB annotations in order to be convertable. Let's consider an example JAXB-annotated class:
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.NONE) public class Employee { @XmlElement(name = "name") public String getName() { return "John Doe"; } }
Now you want to test how it works with real data after convertion to XML (in a unit test):
import com.jcabi.matchers.JaxbConverter; import com.jcabi.matchers.XhtmlMatchers; import org.junit.Assert; import org.junit.Test; public final class EmployeeTest { @Test public void testObjectToXmlConversion() throws Exception { final Object object = new Employee(); Assert.assertThat( JaxbConverter.the(object), XhtmlMatchers.hasXPath("/employee/name[.='John Doe']") ); } }
public static Source the(Object object, Class<?>... deps) throws JAXBException
The method will throw AssertionError
if marshalling of
provided object fails for some reason.
The name of the method is motivated by
xmlatchers project
and their XmlMatchers.the(String)
method. Looks like this name
is short enough and convenient for unit tests.
object
- The object to convertdeps
- Dependencies that we should take into accountJAXBException
- If an exception occurs insideCopyright © 2012–2017 jcabi.com. All rights reserved.