如何模拟原始 XML 输出以在 Java 测试类中使用?

发布于 2025-01-14 23:50:59 字数 578 浏览 1 评论 0原文

我正在为应用程序编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2025-01-21 23:50:59

假设 Document 来自 JDOM 库:

您需要以下依赖项:

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

然后解析它:

  import org.jdom2.Document;
  import org.jdom2.input.SAXBuilder;


  String FILENAME = "src/main/resources/staff.xml";

  SAXBuilder sax = new SAXBuilder();

  Document doc = sax.build(new File(FILENAME));

  Mockito.when(m1()).thenReturn(doc);

参考:https://mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/


那么文档说明你可以使用输入流:

http://www.jdom.org/docs/ apidocs/org/jdom2/input/SAXBuilder.html
例子:

String yourXmlString = "<xml>";

InputStream stringStream = new ByteArrayInputStream(yourXmlString .getBytes("UTF-8"));

SAXBuilder sax = new SAXBuilder();
 
Document doc = sax.build(stringStream );

Assuming Document is from JDOM Library:

You need the following dependency:

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Then you parse it:

  import org.jdom2.Document;
  import org.jdom2.input.SAXBuilder;


  String FILENAME = "src/main/resources/staff.xml";

  SAXBuilder sax = new SAXBuilder();

  Document doc = sax.build(new File(FILENAME));

  Mockito.when(m1()).thenReturn(doc);

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:

String yourXmlString = "<xml>";

InputStream stringStream = new ByteArrayInputStream(yourXmlString .getBytes("UTF-8"));

SAXBuilder sax = new SAXBuilder();
 
Document doc = sax.build(stringStream );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文