Superior unit testing with JUnit 5, Mockito, and Hamcrest

Learn extra at:

  • Objects: equalTo, hasToString, instanceOf, isCompatibleType, notNullValue, nullValue, sameInstance
  • Textual content: equalToIgnoringCase, equalToIgnoringWhiteSpace, containsString, endsWith, startsWith
  • Numbers: closeTo, greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo
  • Logical: allOf, anyOf, not
  • Collections: array (examine an array to an array of matchers), hasEntry, hasKey, hasValue, hasItem, hasItems, hasItemInArray

The next code pattern exhibits just a few examples of utilizing Hamcrest in a JUnit 5 check class.

Itemizing 1. Utilizing Hamcrest in a JUnit 5 check class (HamcrestDemoTest.java)


package deal com.javaworld.geekcap.hamcrest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Check;

import java.util.ArrayList;
import java.util.Listing;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

class HamcrestDemoTest {
    @Check
    @DisplayName("String Examples")
    void stringExamples() {
        String s1 = "Whats up";
        String s2 = "Whats up";

        assertThat("Evaluating Strings", s1, is(s2));
        assertThat(s1, equalTo(s2));
        assertThat("ABCDE", containsString("BC"));
        assertThat("ABCDE", not(containsString("EF")));
    }

    @Check
    @DisplayName("Listing Examples")
    void listExamples() {
        // Create an empty record
        Listing<String> record = new ArrayList();
        assertThat(record, isA(Listing.class));
        assertThat(record, empty());

        // Add a pair gadgets
        record.add("One");
        record.add("Two");
        assertThat(record, not(empty()));
        assertThat(record, hasSize(2));
        assertThat(record, comprises("One", "Two"));
        assertThat(record, containsInAnyOrder("Two", "One"));
        assertThat(record, hasItem("Two"));
    }

    @Check
    @DisplayName("Quantity Examples")
    void numberExamples() {
        assertThat(5, lessThan(10));
        assertThat(5, lessThanOrEqualTo(5));
        assertThat(5.01, closeTo(5.0, 0.01));
    }
}

One factor I like about Hamcrest is that it is extremely straightforward to learn. For instance, “assert that identify is Steve,” “assert that record has dimension 2,” and “assert that record has merchandise Two” all learn like common sentences within the English language. In Itemizing 1, the stringExamples check first compares two Strings for equality after which checks for substrings utilizing the containsString() methodology. An non-obligatory first argument to assertThat() is the “purpose” for the check, which is similar because the message in a JUnit assertion and can be displayed if the check fails. For instance, if we added the next check, we might see the assertion error under it:


assertThat("Evaluating Strings", s1, is("Goodbye"));

java.lang.AssertionError: Evaluating Strings
Anticipated: is "Goodbye"
     however: was "Whats up"

Additionally be aware that we are able to mix the not() logical methodology with a situation to confirm {that a} situation will not be true. In Itemizing 1, we verify that the ABCDE String doesn’t include substring EF utilizing the not() methodology mixed with containsString().

Turn leads into sales with free email marketing tools (en)

Leave a reply

Please enter your comment!
Please enter your name here