GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Error Prone
  • Bug Patterns
  • Docs
  • GitHub

This view

  • Original page
  • New address

GeezerVison is showing errorprone.info — SelfAssertion

SelfAssertion
This assertion will always fail or succeed.

Severity
ERROR

Alternate names: TruthSelfEquals

The problem

When using Truth, if a test subject and the argument to isEqualTo are the same instance (for example assertThat(x).isEqualTo(x)), then the assertion will always pass. Truth implements isEqualTo using [Objects#equal] , which tests its arguments for reference equality and returns true without calling equals() if both arguments are the same instance.

JUnit’s assertEquals (and similar) methods are implemented in terms of Object#equals. However, this is not explicitly documented, so isn’t a contractual guarantee of the assertion methods.

To test the implementation of an equals method, use Guava’s EqualsTester, or explicitly call equals as part of the test.

In our experience, assertThat(x).isEqualTo(x) and similar are more likely to be typos than assertions about an equals method. This alone is sufficient motivation to choose a dedicated approach for testing equals implementations.

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("SelfAssertion") to the enclosing element.

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.