Java JUnit Tutorial - JUnit Assert








Assert has a set of assert methods we can use to check the result.

Assert Class

org.junit.Assert class is declared as follows

public class Assert extends java.lang.Object

This class provides a set of assertion methods useful for writing tests.

Only failed assertions are recorded.

The following lists some of the frequently used methods from Assert class.

  • void assertArrayEquals(expectedArray, resultArray);
    test whether two arrays are equal to each other.
  • void assertEquals(boolean expected, boolean actual)
    Check that two primitives/Objects are equal
  • void assertFalse(boolean condition)
    Check that a condition is false
  • void assertNotNull(Object object)
    Check that an object isn't null.
  • void assertNotSame(boolean condition)
    tests if two object references not point to the same object
  • void assertNull(Object object)
    Check that an object is null
  • void assertSame(boolean condition)
    tests if two object references point to the same object
  • void assertTrue(boolean condition)
    Check that a condition is true.
  • void fail()
    Fails a test with no message.

The following code shows a few methods from the list above.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
public class TestJunit {
   @Test
   public void testAdd() {
      //test data
      int num= 5;
      String str= "Abc";

      //check for equality
      assertEquals("Abc", str);
      
      //check for false condition
      assertFalse(num > 6);

      //check for not null value
      assertNotNull(str);
   }
}




Run the Test case

Here is the code to run the test case defined above.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class Main {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

The code above generates the following result.





Example 2

The following code shows more usage for Assert.

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class TestJunit {
  @Test
  public void testAssertions() {
    // test data
    String str1 = new String("abc");
    String str2 = new String("abc");
    String str3 = null;
    String str4 = "abc";
    String str5 = "abc";
    int val1 = 5;
    int val2 = 6;
    String[] expectedArray = { "one", "two", "three" };
    String[] resultArray = { "one", "two", "three" };

    // Check that two objects are equal
    assertEquals(str1, str2);

    // Check that a condition is true
    assertTrue(val1 < val2);

    // Check that a condition is false
    assertFalse(val1 > val2);

    // Check that an object isn't null
    assertNotNull(str1);

    // Check that an object is null
    assertNull(str3);

    // Check if two object references point to the same object
    assertSame(str4, str5);

    // Check if two object references not point to the same object
    assertNotSame(str1, str3);

    // Check whether two arrays are equal to each other.
    assertArrayEquals(expectedArray, resultArray);
  }
}