Home » Core Java » junit » JUnit assertEquals example

About Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit assertEquals example

1. Introduction

To follow through with my previous post about assertTrue and assertFalse, this post will tackle on checking for an equality of a conditional statement on the test cases.

There is a method called assertEquals in the JUnit library that can be used to check if two objects is equally defined or not. It can be used to check if a specific instance of an object is expected on a method called by the test, or if na object passed through a method was “polymorphed” correctly. This is to ensure that an object, decorated or not will have the same base properties as it is expected.

See the code example below.

2. The Source


package com.areyes1.jgc.junit.assertequals;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class JUnitAssertEqualsExample {

	private JUnitAssertEqualsServiceExample junitAssertEqualsServiceSample;
	private ServiceObject serviceObject;
	public void setData() {
		serviceObject = new ServiceObject();
		junitAssertEqualsServiceSample = new JUnitAssertEqualsServiceExample();

	public void testAssertEqualsFalse() {
		//	processed the item
		ServiceObject newServiceObject = new ServiceObject();
	public void testAssertEquals() {

	public void testAssertEqualsWithMessage() {
				"Same Object",
	public void testAssertEqualsFalseWithMessage() {
		ServiceObject newServiceObject = new ServiceObject();
				"Not the Same Object",


The assertEquals is basically a function that takes two objects and see if they have the same instance object being used. The Example as shown above has 4 set of test that regresses the assertEquals. It checks for the same object that was processed and see if it’s still the same object in term of it’s instance, as it was passed before. A test case if the object used has a different one, and samples with messages in it.

Running this example will give you an output in Eclipse.

Figure 1.0 Example of assertEquals

Figure 1.0 Example of assertEquals

3. Download the Eclipse project

This was an example of JUnit assertEquals source.

You can download the full source code of this example here: junit-assertequals-example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments