Updated March 31, 2023
Introduction to JUnit assertEquals
JUnit assertEquals is one of the methods in the JUnit Library to verify the equality of two objects. For example, one of the objects may be the program output, and the other may be externally supplied to the code. The intention of the verification is to ensure that the program works as per the desired manner during the test and take action if it is not so.
JUnit is a very powerful testing tool, in an open-source format, used for testing small as well as large blocks of code, and it has a huge collection of libraries to make testing easy and vibrant. assertEquals is one such library, and there are many more libraries under the assert family, and in this article, we will analyze them.
JUnit Assert Equals Class
assertEquals is one of the methods in the assert group that are used in test cases validation. It belongs to the org.unit.Assert class that extends java.lang.Object class. This assert class contains several methods which are useful in developing test cases to track failures.
The earlier version of JUnit, i.e., JUnit 3, had two different classes: the main class junit.framework.Testcase and an inherited class junit.framework.Assert. All the assert methods are called through the junit.framework.Assert class.
From JUnit version 4 onwards, Annotations were used to mark the tests, and there was no need to extend Testcase class which means that the assert methods are not directly available, but these methods could be imported in static mode from the new Assert class, and that is the reason all such methods under the new class are static methods.
The Assert methods can be imported using the statements
import static org.junit.Assert.*;
Post this import; these static methods can be used without prefix.
JUnit assertEquals Usage
Here the two objects are compared for equality. These objects may have integer, floating-point, and Boolean values. If the values are not equal, then AssertError will be thrown, and if the values are equal, it returns true.
assertEquals(a, b) is the exact code used in the comparison of the objects.
- The objects A and B should be of the same data type
- Objects A and B will get converted to their wrapper object type
- Value in object A will be compared with B.
- If values are equal, it will return true; otherwise, it will trigger failure.
JUnit Assert Methods
The class org.junit.Assert class contains the following methods in it, which can be called in to test various conditions.
assertArrayEquals
Method Name | Access Specifier Return Type | Details |
assertArrayEquals – byte[ ] | Static void | Verifies bytes arrays (a, b) are equal |
assertArrayEquals – char[ ] | Static void | Verifies char arrays (a, b) are equal |
assertArrayEquals – double[ ] | Static void | Verifies double arrays (a, b) are equal |
assertArrayEquals – float[ ] | Static void | Verifies float arrays (a, b) are equal |
assertArrayEquals – int[ ] | Static void | Verifies integer arrays (a, b) are equal |
assertArrayEquals – long[ ] | Static void | Verifies long arrays (a, b) are equal |
assertArrayEquals – objects[ ] | Static void | Verifies object arrays (a, b) are equal |
assertArrayEquals – short[ ] | Static void | Verifies short arrays (a, b) are equal |
The exact syntax is
assertArrayEquals ( xxxx, expecteds, xxxx, actuals )
xxxx – Byte, Char, Double, Float, Int, Long, Objects, Short
assertArrayEquals ( String, message, xxxx, expecteds, xxxx, actuals) - With message
xxxx – Byte, Char, Double, Float, Int, Long, Objects, Short
assertEquals
Access Specifier Return Type | Method Name /
Detail of the method |
static Void | assertEquals ( double expected, double actual )
Verifies whether variables (a, b) with floating-point type are equal. This functionality is not in use. Instead of this, a new function assertEquals ( double expected, double actual, double epsilon ) is used |
static void | assertEquals ( double expected, double actual, double delta )
Verifies whether variables (a, b) with floating-point type are equal within a positive delta value |
static void | assertEquals ( long expected, long actual )
Verifies whether variables (a,b) with long integers type are equal |
static void | assertEquals (objects [ ] expecteds, objects [] actuals )
Verifies whether multiple objects(a, b) are equal. It is not in use. Instead, it is recommended to use assertArrayEquals |
static void | assertEquals (object expected, object actual )
Verifies whether objects (a, b) are equal. |
assertEquals (With Message)
Access Specifier Return Type |
Method Name / Detail of the method |
static Void | assertEquals ( String message, double expected, double actual )
Verifies whether variables (a, b) with floating-point type are equal. This functionality is not in use. Instead of this, a new function assertEquals ( String message, double expected, double actual, double epsilon ) is used |
static void | assertEquals ( String message, double expected, double actual, double delta )
Verifies whether variables (a, b) with floating-point type are equal within a positive delta value |
static void | assertEquals ( String message, long expected, long actual )
Verifies whether variables (a,b) with long integers type are equal |
static void | assertEquals ( String message, objects [ ] expected, objects [] actuals )
Verifies whether multiple objects(a, b) are equal. It is not in use. Instead, it is recommended to use assertArrayEquals |
static void | assertEquals ( String message, object expected, object actual )
Verifies whether objects (a, b) are equal. |
Others
Access Specifier Return Type |
Method Name / Detail of the method |
static void | assertFalse (boolean condition)
Verifies whether the given condition is false |
static void | assertFalse (String message, boolean condition)
Verifies whether the given condition is false (with a message) |
static void | assertNotNull(String message, Object object)
Verifies whether the given object is not null |
static void | assertNotsame(String message, Object unexpected Object actual)
Verifies whether the two objects referred are not the same |
static void | assertNull(String message, Object object)
Verifies whether the given object is null |
static void | assertsame(String message, Object expected Object actual)
Verifies whether the two objects referred are the same |
static void | assertTrue (String message, boolean condition)
Verifies whether the given condition is true |
Example of assertEquals
import org.junit.Test;
import static org,junit.Assert.assertEquals; // importing assertEquals method from
// org.junit.Assert class
String firstobject = "Jupiter";
String secondobject = "Jupiter";
assertEquals(firstobject , secondobject);
The above code will return true because both the strings have the same value.
Conclusion – JUnit assertEquals
Methods in Assert class, especially assertEquals, facilitate testing of equality conditions and ensure that an error-free product is delivered on time.
Recommended Articles
This is a guide to JUnit assertEquals. Here we discuss the following methods in it, which can be called in to test various conditions. You may also have a look at the following articles to learn more –