File: TruthIncompatibleType.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 1,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
This assertion using Truth is guaranteed to be true or false based on the types
being passed to it. For example,

```java
Optional<Integer> x = Optional.of(1);
assertThat(x).isNotEqualTo(Optional.of(2L));
```

```java
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:

```java
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`][javadoc].

```java
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.

[javadoc]: http://static.javadoc.io/com.google.guava/guava-testlib/21.0/com/google/common/testing/EqualsTester.html