`assertThat(boolean)` is harmful - violates fluent API principles
See original GitHub issueThis was raised before by @cpovirk https://github.com/google/truth/issues/48 and IMO the suggestion that error prone would catch it is irrelevant since truth doesn’t require error prone.
Truth is a fluent API so the “contract” is that grammatically correct things would work as expected: “assert that something is true” is correct, yet Truth requires a double boolean check.
This looks natural:
assertThat(e.getStatus().getCode() == Status.ALREADY_EXISTS.getCode());
It’s also consistent with how Guava preconditions work, however it’s wrong and will always pass.
The correct (boolean) version is not fluent
assertThat(e.getStatus().getCode() == Status.ALREADY_EXISTS.getCode()).isTrue();
// and yes, it should be
assertThat(e.getStatus().getCode()).isEqualTo(Status.ALREADY_EXISTS.getCode());
Worse it’s terribly difficult to spot and that’s a sign of a bad API.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Generating a Generic Fluent API in Java - arXiv
API is a fluent API that reports incorrect chaining of the API methods as a type error to the API users. Although.
Read more >Spock Framework Reference Documentation
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful...
Read more >Testing - Spring
This chapter covers Spring's support for integration testing and best practices for unit testing. The Spring team advocates test-driven ...
Read more >Groovy Language Documentation
Principles ; Variables vs fields in type inference ... JSR 223 javax.script API ... Boolean values can be stored in variables, assigned into...
Read more >Hibernate Validator 8.0.0.Final - Jakarta Bean Validation ...
Jakarta Bean Validation 3.0 defines a metadata model and API for entity and ... The @NotNull constraint on manufacturer is violated in ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is quite possibly the biggest footgun in the Truth API. We once batted around some crazy ideas to try to catch it at runtime, but we couldn’t find anything that seemed like an improvement. At this point, we’re committed to the existing API. We might want to recommend Error Prone or similar static analysis somewhere prominent.
…for some reason I hadn’t seen the
.isTrue()at the end of your first example, so sorry for missing that!