Skip to content

Naming of method Matchers#contains is ambiguous #140

@hakanai

Description

@hakanai

In code reviews here, people often misread this:

assertThat(collection, contains("x"));

As meaning this: "Assert that the collection contains 'x'".

But what it actually means is this: "Assert that the collection contains only 'x'".

This causes a lot of unnecessary questions to be raised on our code reviews where code was updated to use Hamcrest, just because it reads differently to what it's actually doing.

At the same time, people looking to write new code with Hamcrest are using autocompletion to find appropriate-looking methods, and will often try to use contains before discovering that they really wanted hasItem.

I would suggest containsOnly as a new name, but containsInAnyOrder sort of has the same issue and I am finding it hard to jam the additional word in there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions