File: ChainedAssertionLosesContext.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,503 bytes parent folder | download | duplicates (2)
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
Assertions made _inside the implementation of another [Truth] assertion_ should
use [`check`], not `assertThat`.

Before:

```
class MyProtoSubject extends Subject {
  ...

  public void hasFoo(Foo expected) {
    assertThat(actual.foo()).isEqualTo(expected);
  }
}
```

After:

```
class MyProtoSubject extends Subject {
  ...

  public void hasFoo(Foo expected) {
    check("foo()").that(actual.foo()).isEqualTo(expected);
  }
}
```

Benefits of `check` include:

-   When the assertion fails, the failure message includes more context: The
    message specifies that the assertion was performed on `myProto.foo()`, and
    it includes the full value of `myProto` for reference.
-   If the user of the assertion called `assertWithMessage`, that message, which
    is lost in the `assertThat` version, is shown by the `check` version.
-   `check` makes it possible to test the assertion with [`ExpectFailure`] and
    to use [`Expect`] or [`assume`]. `assertThat`, by contrast, overrides any
    user-specified failure behavior.

[Truth]: https://github.com/google/truth
[`check`]: https://google.github.io/truth/api/latest/com/google/common/truth/Subject.html#check-java.lang.String-java.lang.Object...-
[`ExpectFailure`]: https://google.github.io/truth/api/latest/com/google/common/truth/ExpectFailure.html
[`Expect`]: https://google.github.io/truth/api/latest/com/google/common/truth/Expect.html
[`assume`]: https://google.github.io/truth/api/latest/com/google/common/truth/TruthJUnit.html#assume--