Saturday, July 12, 2025

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 exhibits just a few 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 = "Good day";         String s2 = "Good day";         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 listing         Checklist listing = new ArrayList();         assertThat(listing, isA(Checklist.class));         assertThat(listing, empty());         // Add a pair gadgets         listing.add("One");         listing.add("Two");         assertThat(listing, not(empty()));         assertThat(listing, hasSize(2));         assertThat(listing, comprises("One", "Two"));         assertThat(listing, containsInAnyOrder("Two", "One"));         assertThat(listing, 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 rather simple to learn. For instance, “assert that identify is Steve,” “assert that listing has measurement 2,” and “assert that listing 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 “motive” for the take a look at, which is identical because the message in a JUnit assertion and shall 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 beneath it:

  assertThat("Evaluating Strings", s1, is("Goodbye")); java.lang.AssertionError: Evaluating Strings Anticipated: is "Goodbye"      however: was "Good day"  

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 test that the ABCDE String doesn’t comprise substring EF utilizing the not() technique mixed with containsString().

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles