Part 5- Complete guide for Hamcrest Matchers

What have we learned so far

Part 1 – What is Unit Testing?
https://www.onlyfullstack.com/what-is-unit-testing/

Part 2 – What is JUnit? How to use JUnit?
https://www.onlyfullstack.com/what-is-junit-how-to-setup-junit-in-eclipse/

Part 3 – Annotations used in JUnit
https://www.onlyfullstack.com/part-3-annotations-used-in-junit/

Part 4 – JUnit Assert Methods
https://www.onlyfullstack.com/junit-assert-methods/

Hamcrest Matcher Tutorial

Hamcrest is a framework for writing matcher objects allowing ‘match’ rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation, or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used. This tutorial shows you how to use Hamcrest for unit testing.

To use Hamcrest matchers in JUnit you use the assertThat statement followed by one or several matchers.

Hamcrest is typically viewed as a third generation matcher framework.

The first generation used assert(logical statement) but such tests were not easily readable.
The second generation introduced special methods for assertions, e.g., assertEquals(). This approach leads to lots of assert methods.
Hamcrest uses assertThat method with a matcher expression to determine if the test was successful. See Wiki on Hamcrest for more details.

The Core Matchers

These matchers are used to match core operations or basic operation. These static methods belongs to org.hamcrest.CoreMatchers package.

1.CoreMatchers.is(T value)– comparing two objects with their equals method
@Test
public void compare2Strings_with_is_matcher() {
    String name = "Saurabh";
    String newName = "Saurabh";
    assertThat(name, is(newName));
}

2.CoreMatchers.not(T value) – if the expected and actual are not equal
@Test
public void compare2Strings_notEquals_with_not_Matcher() {
    String name = "Saurabh";
    String newName = "OnlyFullstack";

    assertThat(name, not(newName));
}

3.CoreMatchers.startsWith(String prefix) & CoreMatchers. endsWith(String suffix) – compares if the string starts with or ends with some string.
@Test
public void startsWith_example() {
    String name = "OnlyFullstack";
    String newName = "Only";
    assertThat(name, startsWith(newName));
}

@Test
public void endsWith_example() {
    String name = "OnlyFullstack";
    String newName = "Only";
    assertThat(name, endsWith(newName));
}

     4.CoreMatchers.containsString(String substring) –Compared if the string contains substring or not.

@Test
public void containsString_example() {
    String name = "OnlyFullstack";
    String newName = "Full";
    assertThat(name, containsString(newName));
}

5.CoreMatchers.notNullValue() –Checks if the value is not null
@Test
public void notNullValue_example() {
    String name = "OnlyFullstack";
    assertThat(name, notNullValue());
}

6.CoreMatchers.sameInstance(T target) & CoreMatchers.instanceOf(Class<?> type) –
sameInstance checks if two Objects are of the same instance.
instanceOf  checks if an Object is an instance of a given class
@Test
public void sameInstance_instanceOf_example() {
    String name = "OnlyFullstack";
    assertThat(name, sameInstance(name));
    assertThat(name, instanceOf(String.class));
}

Number Matcher

These matchers are used for numbers. These static methods belong to org.hamcrest.Matchers package.

1.Matchers.greaterThan(T value) Matchers.greaterThanOrEqualTo(T value) –To check greater than and greater than or equal to condition
@Test
public void greaterThan_greaterThanOrEqualTo_example() {
    assertThat(1, greaterThan(0));
    assertThat(4, greaterThanOrEqualTo(4));
}

2.Matchers.lessThan(T value) Matchers.lessThanOrEqualTo(T value) –To check less than and less than or equal to condition
@Test
public void lessThan_lessThanOrEqualTo_example() {
    assertThat(1, lessThan(6));
    assertThat(4, lessThanOrEqualTo(4));
}

Collection Matcher

These matchers are used on Collections. These static methods belong to org.hamcrest.Matchers package.

1.Matchers.empty() – checks if the specified collection is empty
@Test
public void empty_collection_example() {
    List<String> list = new ArrayList<>();
    assertThat(list, empty());
}

2.Matchers.hasSize(int size) and Matchers.hasItem(T item) –
hasSize – checks if the collection has the specified size.
hasItem – checks if the collection has the specified item.
@Test
public void hasSize_example() {
    List<String> list = Arrays.asList("only", "fullstack")

    assertThat(list, hasSize(2));
    assertThat(list, hasItem("only"));
}

3.Matchers. hasKey(K key),  Matchers.hasValue(V value) & Matchers.hasEntry(K key, V value)-
hasKey – checks if the map contains the specified key
hasValue – checks if the map contains the specified value
hasEntry – checks if the map contains the specified entry
@Test
public void hasKey_hasValue_hasEntry_example() {
    Map<String, String> map = new HashMap<>();
    map.put("only", "fullstack");

    assertThat(map, hasKey("only"));
    assertThat(map, hasValue("fullstack"));
    assertThat(map, hasEntry("only,", "fullstack"));
}

Source Code
Download the source code of JUnit tutorial from below git repository :
unit-testing-and-integration-testing-with-spring-boot

Let’s go to our next tutorial where we will discuss below points :

Part 6 – Assert for an exception in JUnit

In this tutorial, we will understand below topics-
– How do you assert that a certain exception is thrown in JUnit 4 tests?
1. try-catch idiom
2. @Test expected annotation
3. Junit @Rule

https://www.onlyfullstack.com/assert-for-exception-in-junit/

JUnit Tutorial