In Eclipse, how can I see input in Assert.assertEquals when it fails?

I am not a big guru in Eclipse, so please forgive my awkwardness.

In Eclipse, when I call Assert.assertEquals (obj1, obj2) and it doesn’t help, how do I get the IDE to show me obj1 and obj2?

I am using JExample , but I think this should not make any difference.


Edit : this is what I see:

(source: yfrog.com )
,

+3
source share
6 answers

If the information in the JUnit view is not enough for you, you can always set an exception checkpoint, for example, java.lang.AssertionError. When the test starts, the debugger will be stopped immediately before the exception is actually thrown.

+4
source
  • Comparing with failure tracing is not an easy task when your object is a bit more complex.
  • Comparing with the debugger is useful if you have not redefined toString (). This is still very tiring, as you have to check with your own eyes all the objects on both sides.

The Junit Eclipse plugin offers an option on failure: "Compare actual with expected testing." The view is quite close to the classic content comparison tools: Comparison with string objects


The problem is that it is available only when you write assertEquals() with String objects (in the screenshot we see that the option in the corner is not offered without the String class): Comparison without String objects

You can use toString() for your object in a statement, but this is not a good solution:

  • firstly, you correlate toString() with equals(Object) ... a modification of one should entail a change in the other.
  • secondly, semantics are no longer respected. toString() should return a useful method for debugging the state of a single object, and not for identifying an object in the semantics of Java ( equals(Object) ).

In my opinion, I think the JUnit Eclipse plugin is missing a function.
When the comparison fails, even if we are comparing String objects, it should offer a comparison of two objects that rely on their toString() method.
It can offer a minimal visual way of comparing two uneven objects.
Of course, since equals(Object) does not necessarily correlate with toString() , the highlighted differences should be studied by our eyes, but this will already be a very good foundation, and in any case it is much better than no comparison tool.

+6
source

Assert.assertEquals() will put the toString() view of the expected and actual object in the AssertionFailedError message it generates, and eclipse will display this in the "fault trace" part of the JUnit view:

alt text
(source: ibm.com )

If you have complex objects that you want to test, you will have to use a debugger and put an Assert.assertEquals() breakpoint in Assert.assertEquals()

+3
source

What do you see?

When you execute assertTrue () and it fails, you see a null value.

But when you do assertEquals, he should show you what he expected and what he actually received.

If you are using JUnit, make sure you look at the JUnit view and move the mouse to a failed test.

+1
source

FEST Assert displays a comparison dialog box in the event of a claim failure, even if the objects you are comparing are not strings. I explained this in more detail on my blog .

+1
source

If what you are comparing is String, then you can double-click the stack element and display a dialog showing diff in eclipse.

This only works with strings. In the general case, the only way to see the real reason is to set a breakpoint and switch to it.

0
source

Source: https://habr.com/ru/post/1299258/


All Articles