What is Difference Detween assertEquals() vs assertTrue() in TestNG


TestNG supports Assertion class that is used for validation. assertTrue() and assertEquals() are supported functions of this class.

assertEquals()

This function is used to assert whether 2 data values are equal or not. Data types can be used to assert are String, int, Boolean etc. The user should make sure both of values are same data type otherwise it will throw the exception.

Syntax is:

assertEquals(actual, expected)

It accepts 2 parameters − actual and expected to assert values.

When given 2 data are same, assertion gets passed without any exception. However, if both of data are no same it throws error.

assertTrue()

This function is used to assert whether given Boolean condition is true or not. It accepts expression that returns Boolean value. It gets passed if Boolean condition is true else it throws error.

Syntax is:

assertTrue(Boolean_condition)

It accepts only the expression those returns either true or false.

Differences

In practical scenarios, user can use either of these assertions to validate.

For Ex: if user wants to validate value of 2 string variables, either of these can be used.

Assert.assertEquals(a,b)

OR,

Assert.assertTrue(a.equals(b))

  • If a and b are same, we won’t find any differences in these 2 assertions but when there is a failure, or these variables are not same we can notice the differences in error messages.

    assertEquals() throws clear message about exception as shown below:

    java.lang.AssertionError: expected [2] but found [1]

    Expected :2

    Actual :1

    assertTrue() throws error message about exception but it’s not clear as shown below:

    java.lang.AssertionError: expected [true] but found [false]

    Expected :true

    Actual :false

  • assertEquals is null safe while assertTrue is not null safe i.e. that while comparing 2 values we can pass null values to assertEquals but can’t pass null value to assertTrue.

Example

The following code for common TestNG class - OrderofTestExecutionInTestNG:

src/ OrderofTestExecutionInTestNG.java

import org.testng.Assert;
import org.testng.annotations.*;

public class OrderofTestExecutionInTestNG {

    // test case 1
    @Test()
    public void testCase1() {

        System.out.println("in test case 1 for assertEqulas as passed");
        Assert.assertEquals(1,1);
    }
    // test case 2
    @Test
    public void testCase2() {

        System.out.println("in test case 2 for assertTrue as passed");
        Assert.assertTrue("1".equals("1"));
    }
    @Test()
    public void testCase3() {

        System.out.println("in test case 3 for assertEqulas as failed");
        Assert.assertEquals(1,2);
    }
    // test case 4
    @Test
    public void testCase4() {

        System.out.println("in test case 4 for assertTrue as failed");
        Assert.assertTrue("1".equals("2"));
    }

} 

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1" >
    <test name = "test1">
        <classes>
            <class name = "OrderofTestExecutionInTestNG"/>
        </classes>
    </test>
</suite>

Output

in test case 1 for assertEqulas as passed
in test case 2 for assertTrue as passed
in test case 3 for assertEqulas as failed

java.lang.AssertionError: expected [2] but found [1]
Expected :2
Actual   :1

in test case 4 for assertTrue as failed

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false

===============================================
Suite1
Total tests run: 4, Passes: 2, Failures: 2, Skips: 0
=============================================== 

Updated on: 21-Aug-2023

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements