如何在编组JUnit测试时强制执行JAXBEException
在为JUnit测试进行编组时,我还没有找到强制执行JAXBEException的方法。有人有什么想法吗 这是我的编组代码:如何在编组JUnit测试时强制执行JAXBEException,jaxb,Jaxb,在为JUnit测试进行编组时,我还没有找到强制执行JAXBEException的方法。有人有什么想法吗 这是我的编组代码: public String toXml() { log.debug("Entered toXml method"); String result = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
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;
}
在
封送
操作期间,有不同的方法创建jaxbeexception
:
1-封送无效对象
在封送处理操作期间,您可以通过封送JAXBContext
不知道的类的实例来生成JAXBException
(即,以您的示例为例,使用它封送Foo
的实例)。这将产生以下异常
线程“main”javax.xml.bind.JAXBException中的异常:类forum13389277.Foo或其任何超类在此上下文中都是已知的。
位于com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
位于com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.marshall(MarshallerImpl.java:244)
位于javax.xml.bind.helpers.AbstractMarshallerImpl.marshall(AbstractMarshallerImpl.java:95)
位于forum13272288.Demo.main(Demo.java:27)
2-封送到无效输出
如果尝试封送到无效输出,如已关闭的OutputStream
:
FileOutputStream closedStream=newfileoutputstream(“src/foo.xml”);
closedStream.close();
marshall(这个,closedStream);
然后您将得到一个MarshalException
,它是jaxbeexception
的子类
线程“main”javax.xml.bind.MarshalException中的异常
-除此之外:
[java.io.IOException:流已关闭]
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320)
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.marshall(MarshallerImpl.java:244)
位于javax.xml.bind.helpers.AbstractMarshallerImpl.marshall(AbstractMarshallerImpl.java:95)
位于forum13272288.Demo.main(Demo.java:27)
原因:java.io.IOException:流已关闭
位于java.io.FileOutputStream.writeBytes(本机方法)
在java.io.FileOutputStream.write(FileOutputStream.java:318)处
位于com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.flushBuffer(UTF8XmlOutput.java:413)
位于com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.endDocument(UTF8XmlOutput.java:137)
位于com.sun.xml.bind.v2.runtime.output.indentingutf8xmlooutput.endDocument(indentingutf8xmlooutput.java:165)
位于com.sun.xml.bind.v2.runtime.XMLSerializer.endDocument(XMLSerializer.java:852)
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.postwite(MarshallerImpl.java:369)
位于com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:316)
... 3个以上