JUnit – assertThat (Hamcrest) – part 2

Let’s continue (you can find Hamcrest matchers – part 1 here) our series of examples how to write JUnit test with Hamcrest matchers usage.

  • comparesEqualTo()

Creates a matcher of a Comparable object that matches when the examined object is equal to the specified value, as reported by the compareTo method of the examined object. The value which, when passed to the compareTo method of the examined object, should return zero.

   @Test
    public void comparesEqualToExample() {

        assertThat(5, comparesEqualTo(5));
    }

 

  • contains()

Various matchers which can be used to check if an input Iterable contains values. The order of the values is important and the number of items in the Iterable must match the number of values being tested.

Test if the input list contains all of the values, the order of items is important.

  @Test
    public void containsItemsExample() {
        // Given
        List<String> names = Arrays.asList("Jon", "Peter", "Paul");

        // Then
        assertThat(names, contains("Jon", "Peter", "Paul"));
    }

Test if the input list contains items which match all of the matchers in the input matchers list, the order of items is important.

   @Test
    public void containsAllMatchersExample() {
        // Given
        List<String> strings = Arrays.asList("Jon", "Peter", "Paul");

        // Then
        List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<>();
        matchers.add(startsWith("Jo"));
        matchers.add(endsWith("ter"));
        matchers.add(equalTo("Paul"));
        assertThat(strings, contains(matchers));
    }

 

  • containsInAnyOrder()

This one is like contains() but as the name suggest the order of items is not important.

   @Test
    public void containsItemsInAnyOrderExample() {
        // Given
        List<String> names = Arrays.asList("Jon", "Peter", "Paul");

        // Then
        assertThat(names, containsInAnyOrder("Peter", "Jon", "Paul"));
    }

   @Test
    public void containsAllMatchersInAnyOrderExample() {
        // Given
        List<String> strings = Arrays.asList("Jon", "Peter", "Paul");

        // Then
        List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<>();
        matchers.add(endsWith("ter"));
        matchers.add(startsWith("Jo"));
        matchers.add(equalTo("Paul"));
        assertThat(strings, containsInAnyOrder(matchers));
    }

 

  • containsString()

A matcher that matches if the examined String contains the specified String anywhere.

   @Test
    public void containsStringExample() {
        // Given
        String testValue = "A very short sentence.";

        // Then
        assertThat(testValue, containsString("nt"));
    }

 

  • empty()

A matcher for Collections matching examined collections whose isEmpty method returns true.

    @Test
    public void emptyMatcherExample() {
        // Given
        Queue<String> collectionToTest = new LinkedList<>();

        // Then
        assertThat(collectionToTest, is(empty()));
    }

 

  • emptyArray()

A matcher for arrays that matches when the length of the array is zero.

   @Test
    public void emptyArrayMatcherExample() {
        // Given
        Integer[] arrayToTest = new Integer[0];

        // Then
        assertThat(arrayToTest, is(emptyArray()));
    }

 

  • emptyCollectionOf()

A matcher for Collections matching examined collections whose isEmpty method returns true.

   @Test
    public void emptyCollectionOfMatcherExample() {
        // Given
        Set<String> collectionToTest = new HashSet<>();

        // Then
        assertThat(collectionToTest, is(emptyCollectionOf(String.class)));
    }

 

  • emptyIterable()

A matcher for Iterables matching examined iterable that yield no items.

  @Test
    public void emptyIterableMatcherExample() throws Exception {
        // Given
        ArrayList<String> collectionToTest = new ArrayList<>();

        // Then
        assertThat(collectionToTest, is(emptyIterable()));
    }

 

  • emptyIterableOf()

A matcher for Iterables matching examined iterable that yield no items and is of the given type.

   @Test
    public void emptyIterableOfMatcherExample() throws Exception {
        // Given
        ArrayList<String> collectionToTest = new ArrayList<>();

        // Then
        assertThat(collectionToTest, is(emptyIterableOf(String.class)));
    }

 

 

Leave a Reply