Skip to content

assertThat(boolean) is harmful - violates fluent API principles #614

@clehene

Description

@clehene

This was raised before by @cpovirk #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions