Xml representation of expected result for Junit test

I am writing a Junit test in which the expected result is an XML string. What is the best way to present the expected string? Right now I have it like stringBuilder and I am doing the following. I want to argue that the actual and expected are the same. What would be the most efficient way to represent this expected string? Also, I don't want anyone to modify the expected string, so it should also be final.

@Before
public void setUp() {
    expected.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    expected.append("<n-relationship guid=\"IEEBFAD40BC5711DFAE41F5F92790F896\" control=\"add\">");
    expected.append("<n-relbase>ID17A8B10BC5711DFAE41F5F92790F896</n-relbase><n-reltype>IPLS</n-reltype>");
    expected.append("<ip.content><pub.no>1234567</pub.no>");
    expected.append("<event.date>2010-09-10</event.date><event.code>02394802323</event.code>");
    expected.append("<event.treatment>+</event.treatment><event.description>OPPOSITION DISMISSED</event.description>");
    expected.append("</ip.content></n-relpayload></n-relationship");
}
+3
source share
4 answers

a) XML does not belong inside java code. Put it in the same package (preferably in the src / main / resources folder if you are using maven) and load it as a resource.

b) XML: XML. - .

+3

, - , , . ,

  • (& apos;, ..).

- XML , ​​ . XML .

XML- - , , .

, , .

+1

2 XML . , XML:

<foo>
  <bar>xxx</bar>
  <baz>yyy</baz>
</foo>

:

<foo>
  <baz>yyy</baz>
  <bar>xxx</bar>
</foo>

?

If so, then I suggest you use XMLUnit to test your current XML with the expected.

As for the expected XML, the best way is to save them as external files and load them during the test (for example, in a fragment @BeforeClass).

+1
source

I think you are on the right track - define the string constant "EXPECTED_XML" and simply undo it.

0
source

Source: https://habr.com/ru/post/1772698/


All Articles