Table of contents
1.
Introduction
2.
Example
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

JUnit Assertions

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.

import org.junit.jupiter.api.*;
You can also try this code with Online Java Compiler
Run Code

The Assert class in JUnit provides the developer with a set of various Assertion methods to choose from. The developer can record the failed or negative Assertions in JUnit. The Assert class is provided in the Java lang package,

public class assertExample extends java. lang.Object

Example

First, we will create our Java file and name it assertionExample.java.

assertionExample.java

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

public class assertionExample {
   @Test
   public void funcAssertions() {
      //test data
      String str1 = new String ("some text");
      String str2 = new String ("some text");
      String str3 = null;
      String str4 = "some text";
      String str5 = "some text";
    
      int num1 = 12;
      int num2 = 20;

      String[] arr1 = {"first", "second", "third"};
      String[] arr2 =  {"first", "second", "third"};

      //will check the objects are equal
      assertEquals(str1, str2);

      //will check the condition is true
      assertTrue (num1 < num2);

      //will check the condition is false
      assertFalse(num1 > num2);

      //will check the object isn't null
      assertNotNull(str1);

      //will check the object is null
      assertNull(str3);

      //will check if the objects point to the same object
      assertSame(str4,str5);

      //will check if the objects don’t point to the same object
      assertNotSame(str1,str3);

      //will check if the arrays are equal to each other.
      assertArrayEquals(arr1, arr2);
   }
}
You can also try this code with Online Java Compiler
Run Code

Second, we will create our runTest.java file to execute the test cases.

runTest.java

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

public class runTest {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(funcAssertions.class);
      
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      
      System.out.println(result.wasSuccessful());
   }
} 
You can also try this code with Online Java Compiler
Run Code

In the above example, we used multiple assertion methods and passed various values to the functions to check the functioning of each.

Check out JUnit Interview Questions here.

FAQs

  1. What is JUnit?
    JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
     
  2. What is the latest version of JUnit?
    The most recent version of JUnit is Junit5, although the previous versions of Junit are still equally famous. For instance, JUnit4 has over 100,000 usages.
     
  3. What is Dynamic Testing?
    A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method.
     
  4. What is camel casing?
    Camel casing is the practice of writing words without spaces or punctuations. The separation of words is indicated using capital letters. The first letter starts with either case. For example, onlyNumOrAlpha, ifNumOrAlpha.
     
  5. How can we use the Assertion Functions?
    The Assert class in JUnit provides the developer with a set of various Assertion methods to choose from. The developer can record the failed or negative Assertions in JUnit. The Assert class is provided in the Java lang package.

Key Takeaways

This Blog covered all the necessary points about Assertions and all the various Assertion methods in JUnit. We further discussed how to implement Assertion in JUnit using an example.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass