views:

64

answers:

2

Hi,

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 ?

+1  A: 

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

Oded
A: 

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
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
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
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
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