Skip to content

hasSize and similar matchers to provide more debugging info #174

Open

Description

@alb-i986

Consider the following test case:

@Test
public  void asd() {
    assertThat(Arrays.asList(57, 965), hasSize(greaterThan(3)));
}

When this guy fails, we'll get:

java.lang.AssertionError: 
  Expected: a collection with size a value greater than <3>
    but: collection size <2> was less than <3>

Which is ok, but it doesn't provide much info in order to debug the failure.
A more insightful output, I think, could be this:

java.lang.AssertionError: 
  Expected: a collection with size a value greater than <3>
    but: collection size <2> was less than <3>
    Actual collection:
      - 57
      - 965

By knowing what the array looked like at run time, it's more likely we'll be able to figure out what went wrong by just reading the error message.

This applies to any matcher dealing with the size of some data structure: hasSize, iterableWithSize, arrayWithSize.

In case the actual collection is very large, we should truncate it, as we don't want to pollute the output. We may save the full output to a text file for later consumption by the developer/tester:

java.lang.AssertionError: 
  Expected: a collection with size a value greater than <9000>
    but: collection size <8999> was less than <9000>
    Actual collection:
      - 57
      - 965
      - 4394
      - ... (truncated; please check the full content at /tmp/hamcrest/$timestamp)

Activity

  1. added 5 commits that reference this issue on Apr 19, 2020
    1b6b05a
    29ea395
    d7bc21b
    a5a99c7
    fa60efa
  2. added a commit that references this issue on Aug 23, 2020
    27bb8dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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