How can I find out what caused equals() to return false?
I’m not asking about a sure-way, always right approach, but of something to aid in the development process. Currently I have to step into the equals() calls (usually a tree of them) until one of them is false, then step into it, ad nauseam.
I thought about using the object graph, outputting it to xml and comparing the two objects. However, XMLEncoder requires default constructors, jibx requires pre-compilation, x-stream and simple api are not used in my project. I don’t mind copying a single class, or even a package, into my test area and using it there, but importing a whole jar for this just isn’t going to happen.
I also thought about building an object graph traverser myself, and I might still do it, but I’d hate to start dealing with special cases (ordered collections, non-ordered collections, maps…)
Any idea how to go about it?
Edit: I know adding jars is the normal way of doing things. I know jars are reusable units. However, the bureaucracy needed (at my project) for this doesn’t justify the results – I’d keep on debugging and stepping into.
It’s presumably not a full graph comparison… unless your equals include every property in each class … (you could try == :))
Try hamcrest matchers – you can compose each matcher in an ‘all of’ matcher:
It will say things like: ‘expected myField1 to have a value of ‘value’ but was ‘a different value”
Of course you can inline the static factories. This is a bit heavier than using apache-commons EqualsBuilder, but it does give you an accurate description of exactly what failed.
You can create your own specialised matcher for quickly creating these expressions. It would be wise to copy apache-commons EqualsBuilder here.
BTW, the hamcrest basic jar is 32K (including source!) giving you the option of reviewing the code and saying to your bosses ‘I’ll stand by this as my own code’ (which I presume is your import problem).