TestNG Assertions

Assertions

In test automation, assertions are validations used to compare the actual outcome of the application with the expected outcome. They help determine whether a test has passed or failed. Assertions are essential to ensure that the application behaves as intended.

Assertion Syntax

It is the most commonly used assertion syntax. 

Assert.assertionMethod(actualValue, expectedValue);


actualValue: The result returned by the application under test.
expectedValue: The correct result that the application is expected to return.

Assertion Methods

Here are the common testNG assertions that used in test automation. 

Assertion

Description

Assert.assertEquals(actualValue, expectedValue)

Verifies that the actual value and the expected value are equal

Assert.assertNotEqual(valueOne, valueTwo)

Verifies that valueOne and valueTwo are not equal

Assert.assertTrue(condition)

Verifies the condition is true

Assert.assertFalse(condition)

Verifies that the condition is false

Assert.assertNull(object)

Verifies that the object is null

Assert.assertNotNull(object)

Verifies that the object is not null


Assertion Types

There are two types of Assertions in TestNG: Hard Assertion and Soft Assertion. 

Hard Assertion

     1. It only executes the next line after the Assertion when the assertion condition is true. 
     2. It immediately stops the test execution when the assertion condition fails and marks the test case as failed. 

Example Hard Assertion Script

import org.testng.Assert;
import org.testng.annotations.Test;
public class ForAssertions {
    public void verifyEquals(String actual, String expected) {
        Assert.assertEquals(actual, expected, "Titles do not match");
    }
    public void verifyTrue(boolean condition) {
        Assert.assertTrue(condition, "Condition should be true");
    }
    public void verifyFalse(boolean condition) {
        Assert.assertFalse(condition, "Condition should be false");
    }
    public void verifyNotEquals(int actual, int expected) {
        Assert.assertNotEquals(actual, expected, "Values should not match");
    }
    public void verifyNull(Object obj) {
        Assert.assertNull(obj, "Object should be null");
    }
    public void verifyNotNull(Object obj) {
        Assert.assertNotNull(obj, "Object should not be null");
    }
    @Test
    public void testAllAssertions() {
        verifyEquals("Welcome", "Welcome");
        verifyTrue(true);
        verifyFalse(false);
        verifyNotEquals(5, 10);
        verifyNull(null);
        verifyNotNull("Test");
    }
}


Note: You can modify the values in the testAllAssertions() method to observe how the test behaves when assertions pass or fail.

Soft Assertions

     1. Test execution continues even if some assertions fail and marks the test as failed.
     2. All assertion results are collected and reported at the end.
     3. Requires a final call to assertAll() to aggregate and log the failures.
     4. Useful while validating multiple conditions in a single test without stopping at the first failure.

Soft Assert Syntax

// Step 1: Create a SoftAssert object
SoftAssert softAssert = new SoftAssert();
// Step 2: Perform soft assertions (example method: assertEquals)
// This will not stop the test even if it fails
softAssert.assertEquals(actualValue, expectedValue, "Values do not match");
// Step 3: Final step - collect and report all failures
// This will throw all assertion errors collected above (if any)
softAssert.assertAll();


Example Soft Assertion Script

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class ForSoftAssertions {
    SoftAssert softAssert = new SoftAssert();
    public void verifyEquals(String actual, String expected) {
        softAssert.assertEquals(actual, expected, "Titles do not match");
        System.out.println("verifyEquals executed");
    }
    public void verifyTrue(boolean condition) {
        softAssert.assertTrue(condition, "Condition should be true");
        System.out.println("verifyTrue executed");
    }
    public void verifyFalse(boolean condition) {
        softAssert.assertFalse(condition, "Condition should be false");
        System.out.println("verifyFalse executed");
    }
    public void verifyNotEquals(int actual, int expected) {
        softAssert.assertNotEquals(actual, expected, "Values should not match");
        System.out.println("verifyNotEquals executed");
    }
    public void verifyNull(Object obj) {
        softAssert.assertNull(obj, "Object should be null");
        System.out.println("verifyNull executed");
    }
    public void verifyNotNull(Object obj) {
        softAssert.assertNotNull(obj, "Object should not be null");
        System.out.println("verifyNotNull executed");
    }
    @Test
    public void testAllSoftAssertions() {
        verifyEquals("Welcome", "Welcome");
        verifyTrue(true);
        verifyFalse(false);
        verifyNotEquals(5, 10);
        verifyNull(null);
        verifyNotNull("Test");
        // Final assertion to evaluate all soft failures
        softAssert.assertAll();
    }
}


Note: If softAssert.assertAll() is omitted, failures will not be reported, and the test will appear as passed even if assertions failed.
You can modify the values in the testAllSoftAssertions() method to observe how the test behaves when assertions pass or fail.
 

Related Tutorials