This assertion using Truth is guaranteed to be true or false based on the types being passed to it. For example,
Optional<Integer> x = Optional.of(1); assertThat(x).isNotEqualTo(Optional.of(2L));
ImmutableList<Long> xs = ImmutableList.of(1L, 2L); assertThat(xs).doesNotContain(1);
These will always be true, given
Integer is not comparable to
isn’t such a big issue for
isEqualTo assertions, given the test will fail.
However, it can be insidious for
doesNotContain, given the
assertion will be vacuously true without providing any test coverage.
One false positive for this is where the goal is to test the
implementation of the type under test, i.e. that comparison to a different type
false and does not throw:
For such cases, consider whether your type can be implemented using
to remove the need to implement
equals by hand. If it can’t, consider
new EqualsTester() .addEqualityGroup(MyCustomType.create(1), MyCustomType.create(1)) .addEqualityGroup("") .testEquals();
Although consider omitting an explicit comparison with a different type, as
EqualsTester does this already by default.
Suppress false positives by adding the suppression annotation
@SuppressWarnings("TruthIncompatibleType") to the enclosing element.