Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JUnit - Use assertions


May 15, 2021 jUnit


Table of contents


JUnit - Use assertions

Assertion

All assertions are contained in the Assert class

public class Assert extends java.lang.Object

This class provides many useful assertion methods for writing test cases. O nly failed assertions are logged. eful method columns in the Assert class are as follows:

Serial number Method and description
1 void assertEquals(boolean expected, boolean actual)
Check that the two variables or the equals are balanced
2 void assertTrue(boolean expected, boolean actual)
The check condition is true
3 void assertFalse(boolean condition)
The check condition is false
4 void assertNotNull(Object object)
The check object is not empty
5 void assertNull(Object object)
The check object is empty
6 void assertSame(boolean condition)
The assertSame() method checks whether two related objects point to the same object
7 void assertNotSame(boolean condition)
The assertNotSame() method checks whether two related objects do not point to the same object
8 void assertArrayEquals(expectedArray, resultArray)
The assertArrayEquals() method checks that the two arrays are equal

Let's experiment with the methods mentioned above in the example. I n C: A class JUNIT_WORKSPACE file name TestAssertions is created under the .java path

import org.junit.Test;
import static org.junit.Assert.*;

public class TestAssertions {

   @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);
   }
}

Next, we'll be at C: T his is JUNIT_WORKSPACE a class named TestRunner under the .java to execute the test case

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

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

The Test case and Test Runner classes are compiled with javac

C:\JUNIT_WORKSPACE>javac TestAssertions.java TestRunner.java

Run Test Runner, which will now run the test cases defined and provided in the Test Case class

C:\JUNIT_WORKSPACE>java TestRunner

Check the results of the run

true

Annotations

Annotations are like meta-tags that you can add to your code and apply in a method or class. These annotations in JUnit provide us with information about the test methods, which methods will be applied before and after the test methods, which methods will be applied before and after all methods, and which methods will be ignored in execution.
List of annotations in JUnit and what they mean:

Serial number Notes and descriptions
1 @Test
This annotation illustrates that the public void method attached to JUnit can be used as a test case.
2 @Before
Some tests need to create several similar objects before running. The annotation is added to the public void method because it needs to run before the test method.
3 @After
If you allocate external resources in the Before method, you need to release them after the test runs. The annotation is added to the public void method because it needs to run after the test method.
4 @BeforeClass
The annotation is added to the public void method because it needs to run before all methods in the class.
5 @AfterClass
It will cause the method to execute after all tests have ended. This can be used for cleanup activities.
6 @Ignore
This annotation is used to ignore tests that do not need to be performed.

In C: This JUNIT_WORKSPACE a class named JunitAnnotation under the .java to test the annotations

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class JunitAnnotation {

   //execute before class
   @BeforeClass
   public static void beforeClass() {
      System.out.println("in before class");
   }

   //execute after class
   @AfterClass
   public static void  afterClass() {
      System.out.println("in after class");
   }

   //execute before test
   @Before
   public void before() {
      System.out.println("in before");
   }

   //execute after test
   @After
   public void after() {
      System.out.println("in after");
   }

   //test case
   @Test
   public void test() {
      System.out.println("in test");
   }

   //test case ignore and will not execute
   @Ignore
   public void ignoreTest() {
      System.out.println("in ignore test");
   }
}

Next, we'll be at C: A class JUNIT_WORKSPACE file named TestRunner is created under the .java to perform annotations

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

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

The Test case and Test Runner classes are compiled with javac

C:\JUNIT_WORKSPACE>javac TestAssertions.java TestRunner.java

Run Test Runner, which will now run the test cases defined and provided in the Test Case class

C:\JUNIT_WORKSPACE>java TestRunner

Check the results of the run

in before class
in before
in test
in after
in after class
true