Argument is not compatible with the subject's type.


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);
ImmutableList<Long> xs = ImmutableList.of(1L, 2L);

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:


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))

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.