EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials JUnit Tutorial JUnit assertEquals
Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions

JUnit assertEquals

JUnit assertEquals

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,130 ratings)

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 –

  1. JUnit Maven Dependency
  2. JUnit Annotations 
  3. JUnit version
  4. Java Testing Tools
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more