google / google/truth

Consider adding assertWithMessage(Object) overload

Open
#498 9 comments 0 reactions 0 assignees View on GitHub
P3 type=addition
Dominant language
Java
Stars
2.8k
Forks
275
Avg merge
7m
Merged PRs (30d)
4

Description

When calling into the sub-fields of a composite object, the subject is wrong. Consider:

```java
class Status {
String message;
Code code;
}
```

Calling Truth with `assertThat` is not ideal, because the `Status` subject is lost:

```java
assertThat(status.code).isEqualTo(Code.INVALID_ARGUEMNT);
```

The code may be different, but to debug the test failure, we need to know what the message was. Similarly, checking the message may lose what the code originally was.

Making a custom `Subject.Factory` and `Subject` aren't really appropriate here. They could potentially give better error messages, and more appropriate asserts, but using them makes code very verbose, and precludes reuse. For example, a custom StatusSubject can't reuse the methods available on the StringSubject class.

`assertWithMessage` seems like the right approach, but it takes Object rather than String, which runs into the verbosity problem again. Calling assertWithMessage(status.toString()) is really long, and annoying to do if making multiple assertions. To ameliorate the issue, I propose making an assertWithMessage overload, which takes an Object. This would make it so the correct subject can be printed when an exception occurs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.