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 ofmyProtofor reference. - If the user of the assertion called
assertWithMessage, that message, which is lost in theassertThatversion, is shown by thecheckversion. checkmakes it possible to test the assertion withExpectFailureand to useExpectorassume.assertThat, by contrast, overrides any user-specified failure behavior.