Skip to content

Invert equality check? #3120

@shmosel

Description

@shmosel

When comparing abstractions (e.g. Map) via assertEquals(expected, actual), it's more likely that actual is a custom class. If there's a bug in the equals() implementation, it could be caught by calling actual.equals(expected) instead of the inverse.

For example, here's an implementation of Map.Entry that violates the equals() contract:

class EmptyEntry<K, V> implements Map.Entry<K, V> {
    @Override public K getKey() { return null; }
    @Override public V getValue() { return null; }
    @Override public V setValue(V value) { return null; }
}

This assertion passes:

assertEquals(new AbstractMap.SimpleEntry<>(null, null), new EmptyEntry<>());

This assertion fails:

assertEquals(new EmptyEntry<>(), new AbstractMap.SimpleEntry<>(null, null));

Shouldn't it work the other way?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions