How to get JAXBException when sorting for JUnit test

I could not find a way to throw a JAXBException when sorting for a JUnit test. Does anyone have any ideas?

Here is my sort code:

public String toXml() { log.debug("Entered toXml method"); String result = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(Config.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter writer = new StringWriter(); jaxbMarshaller.marshal(this, writer); result = writer.toString(); } catch (JAXBException e) { log.error(e); } log.debug("Exiting toXml method"); return result; } 
+6
source share
1 answer

There are various ways to JAXBException during a marshal operation:

1 - Marshal Invalid object

You can JAXBException during a marshal operation by combining an instance of a class that JAXBContext does not know about (i.e. take your example and use it to marshal the Foo instance). This will result in the following exception.

 Exception in thread "main" javax.xml.bind.JAXBException: class forum13389277.Foo nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95) at forum13272288.Demo.main(Demo.java:27) 

2 - Marshal to an unacceptable conclusion

If you try to marshal invalid output, such as a private OutputStream :

  FileOutputStream closedStream = new FileOutputStream("src/foo.xml"); closedStream.close(); jaxbMarshaller.marshal(this, closedStream); 

You will then get a MarshalException , which is a subclass of JAXBException .

 Exception in thread "main" javax.xml.bind.MarshalException - with linked exception: [java.io.IOException: Stream Closed] at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95) at forum13272288.Demo.main(Demo.java:27) Caused by: java.io.IOException: Stream Closed at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:318) at com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.flushBuffer(UTF8XmlOutput.java:413) at com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.endDocument(UTF8XmlOutput.java:137) at com.sun.xml.bind.v2.runtime.output.IndentingUTF8XmlOutput.endDocument(IndentingUTF8XmlOutput.java:165) at com.sun.xml.bind.v2.runtime.XMLSerializer.endDocument(XMLSerializer.java:852) at com.sun.xml.bind.v2.runtime.MarshallerImpl.postwrite(MarshallerImpl.java:369) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:316) ... 3 more 
+3
source

All Articles