JUnit – assertThat (Hamcrest) – part 3

Ok, it’s time to continue our series of examples Hamcrest matchers usage. Previous blog posts can be found here (part 1part 2).

  • endsWith()

Creates a matcher that matches if the examined String ends with the specified String.

   @Test
    public void endsWithExample() {

        assertThat("Let's test endsWith matcher", endsWith("matcher"));
    }

 

  • equalTo()

Creates a matcher that matches when the examined object is logically equal to the examined object.

   @Test
    public void equalToValueExample() {

        String testValue = "text";

        assertThat(testValue, equalTo("text"));
    }

 

equalTo() can also be used on Arrays in which case it will check the length of the Array and ensure that all the values in the input test array are logically equal to the values of the specified array.

   @Test
    public void equalToArrayExample() {

        assertThat(new String[] {"Jon", "Paul"}, equalTo(new String[] {"Jon", "Paul"}));
    }

 

  • equalToIgnoringCase()

Creates a matcher of String that matches when the examined string is equal to the specified expectedString, ignoring case.

   @Test
    public void equalToIgnoringCaseExample() {

        String testValue = "TeXt";

        assertThat("text", equalToIgnoringCase(testValue));
    }

 

  • equalToIgnoringWhiteSpace()

Creates a matcher of String that matches when the examined string is equal to the specified expected String when whitespace differences are (mostly) ignored. To be exact, the following whitespace rules are applied:

  1. all leading and trailing whitespace of both the expected String and the examined string are ignored
  2. any remaining whitespace, appearing within either string, is collapsed to a single space before comparison
   @Test
    public void equalToIgnoringWhiteSpaceExample() {

        String testValue = "all leading    and trailing whitespace of both    the       expectedString and     the examined string   are       ignored    ";

        // Then
        assertThat(testValue, equalToIgnoringWhiteSpace("all leading and trailing whitespace of both the expectedString and the examined string are ignored"));
    }

 

  • greaterThan()

Creates a matcher of Comparable object that matches when the examined object is greater than the specified value, as reported by the compareTo method of the examined object.

   @Test
    public void greaterThanExample() {

        Integer testValue = 5;

        assertThat(6, is(greaterThan(testValue)));
    }

 

  • greaterThanOrEqualTo()

Creates a matcher of Comparable object that matches when the examined object is greater than or equal to the specified value, as reported by the compareTo method of the examined object.

    @Test
    public void greaterThanOrEqualToExample() {

        Double testValue = 5.5;

        assertThat(5.5, is(greaterThanOrEqualTo(testValue)));
    }

 

  • hasEntry()

Creates a matcher for Maps matching when the examined Map contains at least one entry whose key equals the specified key and whose value equals the specified value.

  @Test
    public void hasEntryExample() {

        Map<Integer, String> myMap = new HashMap<>();
        myMap.put(1, "value");

        assertThat(myMap, hasEntry(1, "value"));
    }

 

  • hasKey()

Creates a matcher for Maps matching when the examined Map contains at least one key that satisfies the specified matcher.

  @Test
    public void hasKeyExample() {

        Map<Integer, String> myMap = new HashMap<>();
        myMap.put(1, "value");
        myMap.put(2, "anotherValue");

        assertThat(myMap, hasKey(1));

 

  • hasValue()

Creates a matcher for Maps matching when the examined Map contains at least one value that is equal to the specified value.

   @Test
    public void hasValueExample() {

        Map<Integer, String> myMap = new HashMap<>();
        myMap.put(1, "value");
        myMap.put(2, "anotherValue");

        assertThat(myMap, hasValue("anotherValue"));
    }

 

  • hasItem()

Creates a matcher for Iterables that only matches when a single pass over the examined Iterable yields at least one item that is equal to the specified item. Whilst matching, the traversal of the examined Iterable will stop as soon as a matching item is found.

   @Test
    public void hasItemExample() {

        List<Integer> testList = Arrays.asList(50, 60, 4, 67, 9);

        assertThat(testList, hasItem(67));

    }

 

  • hasItems()

Creates a matcher for Iterables that matches when consecutive passes over the examined Iterable yield at least one item that is equal to the corresponding item from the specified items. Whilst matching, each traversal of the examined Iterable will stop as soon as a matching item is found.

    @Test
    public void hasItemsExample() {

        List<Integer> testList = Arrays.asList(50, 60, 4, 67, 9);

        assertThat(testList, hasItems(67, 60));
    }

 

  • hasSize()

Creates a matcher for Collections that matches when the size() method returns a value equal to the specified size.

   @Test
    public void hasSizeExample() {

        List<Integer> testList = Arrays.asList(50, 60, 4, 67, 9);

        assertThat(testList, hasSize(5));
    }

 

  • instanceOf()

Creates a matcher that matches when the examined object is an instance of the specified type.

   @Test
    public void instanceOfExample() {

        Object integer = 5;

        assertThat(integer, instanceOf(Integer.class));
    }

 

 

Leave a Reply