HomeCloud ComputingSuperior unit testing with JUnit 5, Mockito, and Hamcrest

Superior unit testing with JUnit 5, Mockito, and Hamcrest



  • 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 (evaluate an array to an array of matchers), hasEntry, hasKey, hasValue, hasItem, hasItems, hasItemInArray

The next code pattern reveals a number of examples of utilizing Hamcrest in a JUnit 5 take a look at class.

Itemizing 1. Utilizing Hamcrest in a JUnit 5 take a look at class (HamcrestDemoTest.java)


package deal com.javaworld.geekcap.hamcrest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Take a look at;

import java.util.ArrayList;
import java.util.Checklist;

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

class HamcrestDemoTest {
    @Take a look at
    @DisplayName("String Examples")
    void stringExamples() {
        String s1 = "Hiya";
        String s2 = "Hiya";

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

    @Take a look at
    @DisplayName("Checklist Examples")
    void listExamples() {
        // Create an empty record
        Checklist record = new ArrayList();
        assertThat(record, isA(Checklist.class));
        assertThat(record, empty());

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

    @Take a look at
    @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 title 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 take a look at first compares two Strings for equality after which checks for substrings utilizing the containsString() technique. An non-obligatory first argument to assertThat() is the “purpose” for the take a look at, which is similar because the message in a JUnit assertion and might be displayed if the take a look at fails. For instance, if we added the next take a look at, we’d see the assertion error under it:


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

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments