TruthIncompatibleType
Argument is not compatible with the subject's type.

Severity
WARNING

The problem

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 Long. This isn’t such a big issue for isEqualTo assertions, given the test will fail. However, it can be insidious for isNotEqualTo or doesNotContain, given the assertion will be vacuously true without providing any test coverage.

Testing equals methods

One false positive for this is where the goal is to test the equals implementation of the type under test, i.e. that comparison to a different type is false and does not throw:

assertThat(myCustomType).isNotEqualTo("");

For such cases, consider whether your type can be implemented using AutoValue to remove the need to implement equals by hand. If it can’t, consider EqualsTester.

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.

Suppression

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