3

I am writing a JUnit test case which want to test whether a particular file is added with some content or not. In that case, I want to get the instance of the file before modification and another file instance of the same file after modification and want to check whether both are not equal. how to do that in Java Junit ?

Senthil
  • 513
  • 2
  • 13
  • 24

2 Answers2

4

There are tools that exist for this purpose, e.g. http://xmlunit.sourceforge.net/

XMLUnit can ignore whitespace and formatting which I would imagine are immaterial and will also handle comparing

<stuff/>

and

<stuff></stuff> 
David
  • 497
  • 4
  • 10
  • Thanks for the reply. Can I know how to get the instance of the same file at different time. I mean before and after the change occurred in xml file. – Senthil Oct 14 '10 at 09:44
  • You could create a duplicate file prior to the change and then compare the two? See http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java for copying files. Or you could hold the contents in memory if they were small enough. – David Oct 14 '10 at 13:57
  • I have another test case scenario in which I compare two files. One is the actual file which i modify and the another one is expected file ie. the file would have the expected outout. If I use the assertbinaryEquals or assertEquals method of FileAssert class, I am getting testcase failure even if the content in both the files are equal. May I know what is the reason and why it is not working ? – Senthil Oct 15 '10 at 06:08
  • Does the exception thrown (AssertionFailedError) give you any information on this? To hazard a guess, the file attributes (last modified, etc) might be different? I'm afraid I don't know how far the comparison goes. You could avoid using FileAssert altogether if you read the files in as strings. – David Oct 18 '10 at 10:02
0

Get the string representations of the XML (toString) and compare those.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    This is only really useful for very short pieces of XML. When debugging the comparison failure on two large pieces of XML you basically have to copy/paste the output into a diff editor to see where you've gone wrong. Additionally, it doesn't take whitespace or other formatting into account. – Catchwa Mar 02 '11 at 06:49
  • If you don't have a means of taking ignorable whitespace into account, this isn't as helpful as it first seems. – james.garriss Feb 14 '12 at 17:26
  • @james.garriss - That depends on the meaning of "identical", as far as the OP is concerned. – Oded Feb 14 '12 at 17:33