如何模拟原始 XML 输出以在 Java 测试类中使用?
我正在为应用程序编写 Junit 测试用例,我必须一次又一次地构建虚拟 Document 对象
并根据我的原始响应设置根元素和所有其他元素,以便传入我的 Mockito .when(m1()).thenReturn(respDoc)
。代码是这样的
Code Starts
Document respDoc = new Document();
Element elem = new Element("RootElement");
respDoc.setRootElement(elem);
Element node1 = new Element("Nodes").addContent("FirstNode");
elem.add(node1);
**And So On...**
Code Ends
有时响应 xml 太大,以至于我需要花费所有时间来创建这个 Document
目的。有什么方法可以让我传递整个 XML
并提供所需的输出。
如果有任何困惑,请告诉我。 提前致谢!
I am writing Junit test cases for an Application and again and again I have to build dummy Document object
and set root element and every other elements according to my original response so as to pass in my Mockito.when(m1()).thenReturn(respDoc)
. The code is something like this
Code Starts
Document respDoc = new Document();
Element elem = new Element("RootElement");
respDoc.setRootElement(elem);
Element node1 = new Element("Nodes").addContent("FirstNode");
elem.add(node1);
**And So On...**
Code Ends
Sometimes the response xml is so big that it takes all of my time just to create this Document
object. Is there any way where I can just pass this whole XML
and it gives me the desired output.
Please let me know if theres any confusion.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
Document
来自 JDOM 库:您需要以下依赖项:
然后解析它:
参考:https://mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
那么文档说明你可以使用输入流:
http://www.jdom.org/docs/ apidocs/org/jdom2/input/SAXBuilder.html
例子:
Assuming
Document
is from JDOM Library:You need the following dependency:
Then you parse it:
Reference: https://mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
Well the documentation states you can use an InputStream:
http://www.jdom.org/docs/apidocs/org/jdom2/input/SAXBuilder.html
Example: