JUnit assertEquals example

Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.

Please note that you need to use JUnit’s org.junit.Assert class in case of JUnit 4 or JUnit 3 to assert using assertEquals method.

assertEquals usage

Assertions.assertEquals() checks if expected and actual are equal. In case, both are not equal, it will through AssertError.
Please also note that If both are null, they are considered equal.

When you call assertEquals(object1, object2), it use equals method of that type of Object.

For example:
if object1 and object2 are of String type, then String’s equals method will be called to determine equality relation.

In case of Custom objects, it will use custom object’s equals method to determine equality. In case, it does not override equals method, it will use default behaviour of object class i.e. Object identity.

If object1 and object2 are primitives such as boolean, int , long and object1 and object2 will converted to corresponding wrapper classes and object1.equals(object2) will be used.

Floating point Assertions

When you want to compare floating points such as double and float, then you should always use extra parameter delta to avoid rounding off issues while comparing floating points.

public static void assertEquals(double expected, double actual, double delta)

assertEquals overloaded methods

public static void assertEquals(short expected,short actual)
public static void assertEquals(short expected, short actual,String message)
public static void assertEquals(short expected,short actual,Supplier messageSupplier)public static void assertEquals(byte expected,byte actual)
public static void assertEquals(byte expected, byte actual,String message)
public static void assertEquals(byte expected,byte actual,Supplier messageSupplier)public static void assertEquals(int expected,int actual)
public static void assertEquals(int expected, int actual,String message)
public static void assertEquals(int expected,int actual,Supplier messageSupplier)public static void assertEquals(long expected,long actual)
public static void assertEquals(long expected, long actual,String message)
public static void assertEquals(long expected,long actual,Supplier messageSupplier)

public static void assertEquals(char expected,char actual)
public static void assertEquals(char expected, char actual,String message)
public static void assertEquals(char expected,char actual,Supplier messageSupplier)

public static void assertEquals(float expected,float actual)
public static void assertEquals(float expected, float actual,String message)
public static void assertEquals(float expected,float actual,Supplier messageSupplier)
public static void assertEquals(float expected, float actual, float delta)
public static void assertEquals(float expected, float actual, float delta, String message)
public static void assertEquals(float expected, float actual, float delta, Supplier messageSupplier)

public static void assertEquals(double expected,double actual)
public static void assertEquals(double expected, double actual,String message)
public static void assertEquals(double expected,double actual,Supplier messageSupplier)
public static void assertEquals(double expected, double actual, double delta)
public static void assertEquals(double expected, double actual, double delta, String message)
public static void assertEquals(double expected, double actual, double delta, Supplier messageSupplier)

public static void assertEquals(Object expected,Object actual)
public static void assertEquals(Object expected, Object actual,String message)
public static void assertEquals(Object expected,Object actual,Supplier messageSupplier)

assertEquals example

Here is simple example

When you run above testcase, you will get below output:
assertEqualsTestOutput

Let’s understand output of each testcase:


testSquareRootOf25 – Pass

As 5 should be square root of 25, so this testcase will pass.


testSquareRootOf5 – Fail

As square root of 5 is 2.23606797749979 and not 2.236, so this testcase will fail.


testSquareRootOf5WithDelta – Pass

As square root of 5 is 2.23606797749979 within delta range of 0.0001 with respect to 2.236, so this testcase will pass.


JUnit assertEquals custom object example
Let’s use assertEquals to compare custom object.

Create a simple class named Country.

Create another class named CountryHelper which have a method to return highest populated Country.This is the method we are going to test.

Let’s create our testcase now.

When you run above testcase, you will get below output.
AssertEqualsCustomObjectTestOutput
As you can see, we are comparing two countries objects with the help of assertEquals method and As china has highest population, this testcase will pass.
That’s all about Assertions.assertEquals()

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *