5 Reasons why you should use Hamcrest Matchers

Hamcrest Matchers

Hamcrest(anagram of ‘matchers’) is a library which helps in writing better assertions in you tests and is available for various programming languages including Java. It allows the creation of more readable assertions and better failure messages and is also customizable. Here we will see a few reasons why you should use Hamcrest matchers.

Why you should use Hamcrest Matchers

  • More Readable Assertions

    Hamcrest Matchers are more readable compared to other assert statements.
    As a basic example, the statement ‘assertEquals(expected, actual);‘ can be represented using hamcrest matchers as any of the following

    assertThat(actual, equalTo(expected));
    assertThat(actual, is(equalTo(expected)));
    assertThat(actual, is(expected));

    As you can see, they are more readable and can be represented in any manner.

  • Less likely to Interchange Actual & Expected Values

    In normal assertions, there is a specific order for expected and actual (assertEquals(expected, actual) for jUnit) which can easily be interchanged by mistake and can cause confusion in case of failures. This doesn’t occur in hamcrest assertions as the statement follows the normal sentence structure – assertThat(actual, equalTo(expected)).

  • Complex Combinations and Chaining

    It is possible to provide complex assertion combinations using hamcrest easily. Take this example

    assertTrue(string.contains(“someValue”) && string.contains(“anotherValue”));

    This can be easily written as

    assertThat(string, hasItems(“someValue”, “anotherValue”));

    Chaining allows for statements to be negated (not(foo)), combined (either(foo).or(bar)) or mapped to collections (each(foo)).
    Here is an example

    assertThat(“test”, anyOf(is(“test2”), containsString(“te”)));

    Even though such complex statements can be created using basic assertions, it won’t be as readable as hamcrest.

  • Better Failure Messages

    This is one of the important benefits of hamcrest matchers. We can get really informative failure messages using Hamcrest.
    It’s true that the normal assertEquals provides a good failure message giving the expected and actual values. But assertTrue or assertFalse doesn’t give much information as such. But in hamcrest, the failure messages are highly informative.
    Here is an example.

    assertThat(actual, containsString(expected));
    java.lang.AssertionError:
    Expected: a string containing “foo”
    got: “bar”

  • Customizable

    It allows us to implement the Matcher interface and create our own matchers. If the matchers coming out of the box are not enough for your tests, you can create your own matchers to extend the functionality.

Further Reading:

Hope this article was helpful to you. We will be having a tutorial post on Hamcrest which would cover each of the matchers and other combinations. Subscribe to our newsletter to get more such articles once a week via email.

 

SEE ALSO:   WebDriver Extensions & Addons to Speed up your Scripting

You may also like...

1 Response

  1. John Nelson says:

    Very helpfull

Leave a Reply

Your email address will not be published. Required fields are marked *