question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw ComparisonFailure rather than AssertionError for all equals methods

See original GitHub issue

I’d like to propose a change that I think would make the assertEquals methods easier to work with - apologies if I’ve missed something.

When a test fails in Eclipse with assertEquals() and the parameters are not strings, an AssertionError is thrown. This gives a backtrace, but to see what was not equal, I have to copy and paste out the error into another editor to read the top line. Here’s a screen shot:

screen shot 2017-02-03 at 18 49 26

It would be great if could throw a ComparisonFailure. I’d still see pretty much the same failure trace:

screen shot 2017-02-03 at 18 52 05

but I’d be about to double click it/click the “Compare with Expected Results” button and get this:

screen shot 2017-02-03 at 18 52 20

which would be a tremendous help to my work flow.

When looking into this I did think surely Eclipse could do something better, but I don’t think it can. An AssertionError doesn’t have the expected and actual results. They are thrown away by the call to failNotEquals which calls ‘format’ that then concatenates a single string to pass to AssertionError

So looking at assertEquals for Object’s could we just do away with the instanceof checks?

public static void assertEquals(String message, Object expected,
            Object actual) {
        if (equalsRegardingNull(expected, actual)) {
            return;
        }
        if (expected instanceof String && actual instanceof String) {
            String cleanMessage = message == null ? "" : message;
            throw new ComparisonFailure(cleanMessage, (String) expected,
                    (String) actual);
        } else {
            failNotEquals(message, expected, actual);
        }
    }

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kcooneycommented, Feb 4, 2017

Incidentally, the reason why this isn’t so simple is you might get an exception when calling toString() on the expected and/or actual values.

0reactions
j256commented, Jan 4, 2021

Incidentally, the reason why this isn’t so simple is you might get an exception when calling toString() on the expected and/or actual values.

We could put catches around the toString() calls and fallback to Object.toString() if they throw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throw ComparisonFailure rather than AssertionError for all equals ...
I'd like to propose a change that I think would make the assertEquals methods easier to work with - apologies if I've missed...
Read more >
Why junit ComparisonFailure is not used by assertEquals ...
We started with comparing strings because it was obvious how to make the error message more helpful. We never expanded ComparisonFailure to ...
Read more >
ComparisonFailure (JUnit API)
public class ComparisonFailure; extends AssertionError. Thrown when an assertEquals(String, String) fails. Create and throw a ComparisonFailure manually if ...
Read more >
AbstractCharacterAssert (AssertJ fluent assertions 2.9.1 API)
Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks. SELF · usingComparator(Comparator<? super Character> ...
Read more >
Booleans (AssertJ fluent assertions 2.5.0 API) - javadoc.io
AssertionError - if the actual value is not equal to the expected one. This method will throw a org.junit.ComparisonFailure instead if JUnit is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found