unit testing with xml in it

gudii9
gudii9 used Ask the Experts™
on
i have a existing junit file which basicaly using power mock and validating incoming messages to existings system say XYZ.  I see big XML there with bunch of tags as below

String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>>"
                              + "<Msg><uap>2013-09-2
++ "<id><udd>12

now i need to modify above xml with new xml generated using new xsd.

how to do that?

why xml represented as above with concatenation of + in between
any good ends to end example on this

please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Distinguished Expert 2018
Commented:
>>how to do that?

Just set the String message to the new XML.  The "+" you see is string concatenation.

I also do similar things with data that comes to me on multiple lines.  It isn't part of the XML.

If I have letters on three lines like"
a
b
c

Open in new window


And I want a resulting string "abc", I can do:

"a"+
"b"+
"c"

Open in new window


That says: Take the string "a" concatenate it with string "b" and concatenate that with string "c" resulting in "abc".

So if you new xml is
<a>
<b>Hello</b>
<c>world</c>
</a>

Open in new window

to keep it in the same "format" just make it:

String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<a>"+
"<b>Hello</b>"+
"<c>world</c>"+
"</a>"

Open in new window


You could just as easy make it
String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b>Hello</b><c>world</c></a>"

Open in new window


As far as the XML is concerned, they are the same.  Reading them, they are really different.
Miguel OzSenior Software Engineer
Top Expert 2009
Commented:
Another alterantive to strings is to create a sample folder in your project and store your test xml files there. The advantage is that later on in life you could troubleshoot your production xml in your unit test by just replacing the file. (depending of hwo dependent the tests are with your file contents  but I found it handy it there are inputs to a more complex logic)

Author

Commented:
any end to end examples around this.

I did not understand where to break xml and concatenae with +

say my example as below
<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
   <TITLE>Computer Parts</TITLE>
   <PART>
      <ITEM>Motherboard</ITEM>
      <MANUFACTURER>ASUS</MANUFACTURER>
      <MODEL>P3B-F</MODEL>
      <COST> 123.00</COST>
   </PART>
   <PART>
      <ITEM>Video Card</ITEM>
      <MANUFACTURER>ATI</MANUFACTURER>
      <MODEL>All-in-Wonder Pro</MODEL>
      <COST> 160.00</COST>
   </PART>
   <PART>
      <ITEM>Sound Card</ITEM>
      <MANUFACTURER>Creative Labs</MANUFACTURER>
      <MODEL>Sound Blaster Live</MODEL>
      <COST> 80.00</COST>
   </PART>
   <PART>
      <ITEMᡋ inch Monitor</ITEM>
      <MANUFACTURER>LG Electronics</MANUFACTURER>
      <MODEL> 995E</MODEL>
      <COST> 290.00</COST>
   </PART>
</PARTS>

http://www.comptechdoc.org/independent/web/xml/guide/xmlexample.html

how to unit test this is powerpock to parse then validate and generate etc?(before all that how to make it as string)
please advise
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
It appears you need the ENTIRE XML document to be a string so given the little information you provided, here is a guess.


String message ="<?xml version=\"1.0\"?>"+
"<!DOCTYPE PARTS SYSTEM \"parts.dtd\">"+
"<?xml-stylesheet type=\"text/css\" href=\"xmlpartsstyle.css\"?>"+
"<PARTS>"+
"   <TITLE>Computer Parts</TITLE>"+
"   <PART>"+
"      <ITEM>Motherboard</ITEM>"+
"      <MANUFACTURER>ASUS</MANUFACTURER>"+
"      <MODEL>P3B-F</MODEL>"+
"      <COST> 123.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEM>Video Card</ITEM>"+
"      <MANUFACTURER>ATI</MANUFACTURER>"+
"      <MODEL>All-in-Wonder Pro</MODEL>"+
"      <COST> 160.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEM>Sound Card</ITEM>"+
"      <MANUFACTURER>Creative Labs</MANUFACTURER>"+
"      <MODEL>Sound Blaster Live</MODEL>"+
"      <COST> 80.00</COST>"+
"   </PART>"+
"   <PART>"+
"      <ITEMᡋ inch Monitor</ITEM>"+
"      <MANUFACTURER>LG Electronics</MANUFACTURER>"+
"      <MODEL> 995E</MODEL>"+
"      <COST> 290.00</COST>"+
"   </PART>"+
"</PARTS>"

Open in new window


I do agree with the above suggestion to make it flexible.  If possible, change your test code to read the XML from a file on disk.  Then you never have to change the code again.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Given your need my previous comment is correct save the content as XMLfile. You can validate it in code if need when your code reads it. There is no need to make it a string it is just too much work for too little gain.

Author

Commented:
I do agree with the above suggestion to make it flexible.  If possible, change your test code to read the XML from a file on disk.  Then you never have to change the code again.
any end to end example on this?
please advise
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
I've never heard of Powermock before but it appears it has a readFile method.  I would start there.

If you have any documentation for Powermock I would see if there is documentation on readFile.

If you are using Powermock I'm sure you have experts working with you that should be able to offer some assistance.

Not sure if this helps but there is a readeFile example here:
https://gist.github.com/fatmind/4110984

Author

Commented:
It appears you need the ENTIRE XML document to be a string so given the little information you provided, here is a guess.

just need to put in  " " and then add + next to it?
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
>>just need to put in  " " and then add + next to it?

From the sample you posted, yes.  However you need to make sure you do it in the correct order.  It is simple string concatenation.

https://en.wikipedia.org/wiki/Concatenation

Just follow what I posted in #a42480810
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Please use the XML reader:
var reader = XmlReader.Create("Your file full path.xml")
//Read  your files

Open in new window

You can find sample code in the links below:
https://www.dotnetperls.com/xmlreader
http://www.c-sharpcorner.com/UploadFile/167ad2/read-xml-file-using-xml-reader-in-C-Sharp/
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
@Miguel,

Can you confirm Powermock uses C# or .Net?

If it does, I would probably use XmlDocument.Load instead of XmlReader.
https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx

Author

Commented:
to keep it in the same "format" just make it:

eclipse itself is doing it.

So  have made it working as it is without any changes

Author

Commented:
<?xml version='1.1' encoding='UTF-8'?>
<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\"
   targetNamespace=\"aaacm\" xmlns:aaacm=\"aaacm\">

  <xsd:element name=\"BldSt\">

as as i have to replace " with \" everywhere in xml
then paste xml to eclipse and say enter to move each tag to next line to see nice string formatted xml
StringEncode.png

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial