I'm not much of an Eclipse guru, so please forgive my clumsiness.
In Eclipse, when I call Assert.assertEquals(obj1,obj2) and that fails, how do I get the IDE to show me obj1 and obj2?
I'm using JExample, but I guess that shouldn't make a difference.
Edit: Here's what I see:
http://img86.yfrog.com/img86/3002/jexample.png.
Junit Eclipse plugin offers a option when there is a failure : "Compare actual With Expected TestResult". The view is close enough to classic content comparison tools :
Problem is that it is avaiable only when you write
assertEquals()
withString
objects (in the screenshot, we can see that the option in the corner is not proposed with noString
class) :You may use
toString()
on your object in assertion but it's not a good solution :toString()
withequals(Object)
... modification of one must entail modification of the other.toString()
should return a useful method to debug the state of one object, not to identify an object in the Java semantic (equals(Object)
).According to me, I think that the JUnit Eclipse plugin misses a feature.
When comparison fails, even when we compare not
String
objects, it should offer a comparison of the two objects which rely on theirtoString()
method.It could offer a minimal visual way of comparing two unequals objects.
Of course, as
equals(Object)
is not necessarily correlated totoString()
, highlighted differences should be studied with our eyes but it would be already a very good basis and anyway, it is much better than no comparison tool.Assert.assertEquals()
will put thetoString()
representation of the expected and actual object in the message of theAssertionFailedError
it throws, and eclipse will display that in the "failure trace" part of the JUnit view:alt text http://www.ibm.com/developerworks/java/library/j-xp042203/junitAssertionFailure.gif
If you have complex objects you want to inspect, you'll have to use the debugger and put a breakpoint inside
Assert.assertEquals()
What are you seeing?
When you do assertTrue() and it fails, you see a null.
But when you do assertEquals, it is supposed to show you what it expected and what it actually got.
If you are using JUnit, mke sure you are looking at the JUnit view and moving the mouse to the failed test.
If the information in the JUnit view is not enough for you, you can always set a exception breakpoint on, for example, java.lang.AssertionError. When running the test, the debugger will stop immediately before the exception is actually being thrown.
FEST Assert will display comparison dialog in case of assertion failure even when objects you compare are not strings. I explained it in more detail on my blog.
If what you are comparing is a String then you can double click stack element and it will popup a dialog showing the diff in eclipse.
This only works with Strings though. For the general case the only way to see the real reason is to install a breakpoint and step into it.