question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`assertThat(boolean)` is harmful - violates fluent API principles

See original GitHub issue

This 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:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cpovirkcommented, Jul 25, 2019

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.

0reactions
jbduncancommented, Jul 25, 2019

…for some reason I hadn’t seen the .isTrue() at the end of your first example, so sorry for missing that!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found