JUnit4 Assertions

HOME

JUnit provides overloaded assertion methods for all primitive types and Objects and arrays (of primitives or Objects). The parameter order is expected value followed by an actual value. Optionally the first parameter can be a String message that is output on failure. Only failed assertions are recorded. Assertions is a JUnit API or library of functions through which you can verify if a particular logic or condition returns true or false after execution of the test. If it returns false, then an AssertionError is thrown. These assertions are imported from:

import org.junit.Assert.*;

In order to increase the readability of the test and of the assertions itself, it’s always recommended to import statically the respective class.

import static org.junit.Assert.*;
  1. assertEquals()

The assertEquals() assertion verifies that the expected and the actual values are equal. Below is an example of assertion pass.

@Test
public void equalsPositive() {
    String expected = "JUnit";
    String actual = "JUnit";

    assertEquals("Expected and Actual Strings are not equal",expected,actual);
}

Result

If expected and actual values are not equal, an AssertionError without a message is thrown. Below is an example of AssertionError thrown by assertEquals().

@Test
public void equalsNegative() {
    String expected = "JUnit";
    String actual = "JUnit Test";

    assertEquals(expected,actual);
}

Result

2. assertEquals with NULL

If expected and actual are NULL, then they are considered equal in assertEqual().

@Test
public void test2() {

    String expected = null;
    String actual = null;

    assertEquals("Expected and Actual Strings are not null",expected,actual);
}

Result

3. assertNotEquals()

The assertNotEquals() assertion verifies that the expected and the actual values are not equal. Below is an example where expected and actual values are not equal.

 @Test
  public void notEqualsPositive() {
      String expected = "JUnit";
      String actual = "JUnit5";

      assertNotEquals("Expected and Actual Strings are equal",expected,actual);

 }

Result

If expected and actual values are equal, then an AssertionError with a message is thrown. Below is an example of AssertionError thrown by assertNotEquals().

@Test
public void notEqualsNegative() {
   String expected = "JUnit";
    String actual = "JUnit";

    assertNotEquals("Expected and Actual Strings are equal",expected,actual);
}

Result

4. assertArrayEquals()

If we want to assert that two arrays are equals, we can use the assertArrayEquals(). In the below example, two arrays are equal, so there is no AssertionError.

    @Test
    public void arrayEqualsPositive() {
        char[] expected = {'j','u','n','i','t'};
        char[] actual = {'j','u','n','i','t'};

        assertArrayEquals("Expected and Actual Arrays are not equal",expected,actual);

    }

Result

Below is an example of AssertionError thrown by assertArrayEquals(), when arrays are different.

    @Test
    public void arrayEqualsNegative() {
        char[] expected = {'J','U','n','i','t'};
        char[] actual = "JUnit Test".toCharArray();

        assertArrayEquals("Expected and Actual Arrays are not equal",expected,actual);

    }

Result

5. assertNull()

When we want to test if an object is null we can use the assertNull() assertion.

    @Test
    public void nullPositive() {

        String str = null;
        assertNull("String is not null",str);

    }

Result

If the object is not null, then an AssertionError is thrown with the given message. Below is an example of AssertionError thrown by assertNull().

 @Test
 public void nullNegative() {

    String str = "Happy";
    assertNull("String is not null",str);

 }

Result

6. assetNotNull()

If we want to assert that an object should not be null we can use the assertNotNull assertion. Below is an example of object not null.

 @Test
    public void notNullPositive() {

        String str = "Spring";
        assertNotNull("String is null",str);

    }

Result

If the object is null, then an AssertionError is thrown with the given message. Below is an example of AssertionError thrown by assertNotNull().

   @Test
    public void notNullNegative() {

        String str = null;
        assertNotNull("String is null",str);

    }

Result

7. assertFalse()

If we want to verify that the condition is false, we can use assertFalse() assertion.

    @Test
    public void falsePositive() {

        String str1 = "Happy Days";
        String str2 = new String("Summer");
        assertFalse("String 2 is not present in String 1", str1.contains(str2));

    }

Result

If the condition is true, then an AssertionError is thrown with the given message. Below is an example of AssertionError thrown by assertFalse().

    @Test
    public void falseNegative() {

        String str1 = "Happy Days";
        String str2 = new String("Happy");
        assertFalse("String 2 is not present in String 1", str1.contains(str2));

    }

Result

8. assertTrue()

If we want to verify that the condition is true, we can use assertTrue() assertion.

@Test
public void truePositive() {

    String str1 = "Happy Days";
    String str2 = new String("Days");
    assertTrue("String 2 is present in String 1", str1.contains(str2));

}

Result

If the condition is false, then an AssertionError is thrown with the given message. Below is an example of AssertionError thrown by assertTrue().

    @Test
    public void trueNegative() {

        String str1 = "Happy Days";
        String str2 = new String("Healthy");
        assertTrue("String 2 is present in String 1", str1.contains(str2));

    }

Result

10. assertSame()

The assertSame() internally uses operator == to validate if two objects are equal. Despite the two string values are the same, the below test fails. The reason being that the two object references are different.

In the below example, str1 and str2 have same value as well refer to the same memory addresses, so the assertion pass.

@Test
public void samePositive() {

   String str1 = "Happy";
   String str2 = str1;
   assertSame("String1 and String 2 have different object reference",str1, str2);

 }

Result

Both the string objects str1 and str2 have same value, but are referring to the different memory addresses that result in the failure of the assert function.

@Test
public void sameNegative() {

    String str1 = "Happy";
    String str2 = new String("Happy");
    assertSame("String1 and String 2 have different object reference",str1, str2);

    }

Result

11. fail()

The fail assertion fails a test throwing an AssertionFailedError. It can be used to verify that an actual exception is thrown or when we want to make a test failing during its development.

@Test
public void test14() {

    String str1 = "Happy Days";
    String str2 = new String("Happy");
    Assert.fail("Fail this test");
}

Result

What is the difference between assertEquals() and assertSame() assertions?

assertEquals() uses equals() method to compare objects, while assertSame() uses == operator to asserts that two objects refer to the same object.

In the below example, str1 and str2 both have same value. So, the assertion passes here when we use assertEquals().

@Test
public void test10() {

   String str1 = "Happy";
   String str2 = new String("Happy");
   Assert.assertEquals("String1 and String 2 are equal",str1, str2);

}

Result

In the below example, both str1 and str2 have same value but different object reference, so the assertion fails with assertSame().

@Test
public void test11() {

    String str1 = "Happy";
    String str2 = new String("Happy");
    Assert.assertSame("String1 and String 2 have different object reference",str1, str2);
}

Result

In the below example, str1 and str2 have the same object reference. So, now the assertion passes with assertSame().

 @Test
 public void test12() {

    String str1 = "Happy";
    String str2 = str1;
    Assert.assertSame("String1 and String 2 have different object reference",str1, str2);

 }

Result

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s