Part 4 – JUnit Assert Methods

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/

Assert Methods

This class provides a set of assertion methods, useful for writing tests. Only failed assertions are recorded. We will be writing the test cases for below class:


assertEquals()
The assertEquals() method compares two objects for equality, using their equals() method.

@Test
public void assertEquals_example() {
    Employee employeeNew = new Employee();
    employee.setSalary(1000000.0);
    assertEquals("EMPLOYEE OBJECT", employee, employeeNew);
}
If the two objects are equal according to there implementation of their equals() method, the assertEquals() method will return normally. Otherwise, the assertEquals() method will throw an exception, and the test will stop there.

assertTrue() + assertFalse()
The assertTrue() and assertFalse() methods tests a single variable to see if its value is either true or false. Here is a simple example:

@Test
public void assertTrue_assertFalse_example() {

    assertTrue("VALID EMPLOYEE OBJECT", employeeService.isValidEmployee(employee));
    assertFalse("INVALID EMPLOYEE OBJECT", employeeService.isValidEmployee(null));
}
If the isValidEmployee() method returns true, the assertTrue() method will return normally. Otherwise, an exception will be thrown, and the test will stop there.
If the isValidEmployee() method returns false, the assertFalse() method will return normally. Otherwise, an exception will be thrown, and the test will stop there.

assertNull() + assertNotNull()

The assertNull() and assertNotNull() methods test a single variable to see if it is null or not null. Here is an example:

@Test
public void assertNull_assertNotNull_example() {

    assertNotNull(employeeService.getEmployeeFromId(1)); // in EmployeeService we have a map with
                                                                                                 // single entry of key as 1 so here we will get employee object
    assertNull(employeeService.getEmployeeFromId(2)); // We will get null as response
}
If the employeeService.getEmployeeFromId(2) returns null, the assertNull() method will return normally. If a non-null value is returned, the assertNull() method will throw an exception, and the test will be aborted here.
The assertNotNull() method works oppositely of the assertNull() method, throwing an exception if a null value is passed to it, and returning normally if a non-null value is passed to it.
assertSame() and assertNotSame()
The assertSame() and assertNotSame() methods tests if two object references point to the same object or not. It is not enough that the two objects pointed to are equals according to their equals() methods. It must be exactly the same object pointed to.
Here is a simple example:

@Test
public void assertSame_assertNoSame_example() {

    assertSame(employeeService.getEmployeeFromId(1), employeeService.getEmployeeFromId(1));
    assertNotSame(employee, employeeService.getEmployeeFromId(1)); // We will get null as response
}
If the two references point to the same object, the assertSame() method will return normally. Otherwise, an exception will be thrown and the test will stop here.
The assertNotSame() method works oppositely of the assertSame() method. If the two objects do not point to the same object, the assertNotSame() method will return normally. Otherwise, an exception is thrown and the test stops here.
assertThat()
The assertThat() method compares an object to an org.hamcrest.Matcher to see if the given object matches whatever the Matcher requires it to match.
Please find below example where on the
1st assertThat – its taking the  employeeService.getEmployeeFromId(1) and comparing it with employeeService.getEmployeeFromId(1) with the help of is Matcher.
2nd assertThat is taking employeeService.getEmployeeFromId(1) and checking if the object is not null with is(CoreMatchers.notNullValue()) Matcher.
The matcher is a big topic and we will cover them separately.

@Test
public void assertThat_example() {

    assertThat(employeeService.getEmployeeFromId(1), is(employeeService.getEmployeeFromId(1)));
    assertThat(employeeService.getEmployeeFromId(1), is(CoreMatchers.notNullValue()));

}

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 5 – Complete guide for Hamcrest JUnit

In this tutorial we will understand below topics-
– Hamcrest Matcher Tutorial
– The Core Matchers
1. CoreMatchers.is(T value)
2. CoreMatchers.not(T value)
3. CoreMatchers.startsWith(String prefix) & CoreMatchers. endsWith(String suffix)
4. CoreMatchers.containsString(String substring)
5. CoreMatchers.notNullValue()
6. CoreMatchers.sameInstance(T target) & CoreMatchers.instanceOf(Class<?> type)
– Number Matcher
1. Matchers.greaterThan(T value) Matchers.greaterThanOrEqualTo(T value)
2. Matchers.lessThan(T value) Matchers.lessThanOrEqualTo(T value)
– Collection Matcher
1. Matchers.empty()
2. Matchers.hasSize(int size) and Matchers.hasItem(T item)
3. Matchers. hasKey(K key), Matchers.hasValue(V value) & Matchers.hasEntry(K key, V value)

JUnit Tutorial