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.
These matchers are used to match core operations or basic operation. These static methods belongs to org.hamcrest.CoreMatchers package.
@Test public void compare2Strings_with_is_matcher() { String name = "Saurabh"; String newName = "Saurabh"; assertThat(name, is(newName)); }
@Test public void compare2Strings_notEquals_with_not_Matcher() { String name = "Saurabh"; String newName = "OnlyFullstack"; assertThat(name, not(newName)); }
@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)); }
@Test public void notNullValue_example() { String name = "OnlyFullstack"; assertThat(name, notNullValue()); }
@Test public void sameInstance_instanceOf_example() { String name = "OnlyFullstack"; assertThat(name, sameInstance(name)); assertThat(name, instanceOf(String.class)); }
These matchers are used for numbers. These static methods belong to org.hamcrest.Matchers package.
@Test public void greaterThan_greaterThanOrEqualTo_example() { assertThat(1, greaterThan(0)); assertThat(4, greaterThanOrEqualTo(4)); }
@Test public void lessThan_lessThanOrEqualTo_example() { assertThat(1, lessThan(6)); assertThat(4, lessThanOrEqualTo(4)); }
These matchers are used on Collections. These static methods belong to org.hamcrest.Matchers package.
@Test public void empty_collection_example() { List<String> list = new ArrayList<>(); assertThat(list, empty()); }
@Test public void hasSize_example() { List<String> list = Arrays.asList("only", "fullstack") assertThat(list, hasSize(2)); assertThat(list, hasItem("only")); }
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/