---
title: Assertions in TestNG
description: "**Assertions in TestNG** **1. What Are Assertions in TestNG?** Assertions are checks you place in your test methods to verify that the application under test behaves as expected. If an assertion fails, TestNG marks the test as **FAILED** and (for…"
url: "https://vinothqaacademy.com/docs/assertions-in-testng/"
updated: "2025-09-13"
category: TestNG Framework
tags:
  - Testng Framework
---

# Assertions in TestNG

**Assertions in TestNG**

**1. What Are Assertions in TestNG?**

Assertions are checks you place in your test methods to verify that the application under test behaves as expected. If an assertion fails, TestNG marks the test as **FAILED** and (for hard asserts) aborts execution of the current method.

**2. Syntax for TestNG Assertions**

TestNG provides two main assertion styles:

- **Hard Asserts** via the static methods in org.testng.Assert:

```javascript

                Assert.assertTrue(condition);

Assert.assertEquals(actual, expected);

Assert.assertNotNull(object);
```

- **Soft Asserts** via an instance of org.testng.asserts.SoftAssert:

```javascript

                SoftAssert soft = new SoftAssert();

soft.assertTrue(condition);

// … more soft asserts …

soft.assertAll();  // must call at end to report failures
```

**3. How to Use Assertions in TestNG**

```javascript

                import org.testng.Assert;

import org.testng.annotations.Test;

public class AssertionExamples {

  @Test
 public void hardAssertionTest() {

    int sum = 2 + 2;

    Assert.assertEquals(sum, 4);

    String text = "hello";

    Assert.assertTrue(text.startsWith("h"));

  }

}
```

Each failed hard-assert immediately stops that test method and marks it failed.

**4. Using Messages as a Parameter in TestNG Asserts**

You can supply a custom failure message to make reports more descriptive:

```javascript

                @Test
public void todayDateTest() {

  String today = getCurrentDate();

  // if this assertion fails, “Date format is wrong” appears in the report

  Assert.assertEquals(today, "2025-07-05", "Date format is wrong");

}
```

**5. Different Types of Asserts in TestNG**

**Hard Asserts** (org.testng.Assert): stop execution on failure.

**Soft Asserts** (org.testng.asserts.SoftAssert): collect failures and continue; only fail at the final assertAll().

5.1 Hard Asserts

| **Method** | **Fails If…** |
| --- | --- |
| Assert.assertTrue(cond) | cond is false |
| Assert.assertFalse(cond) | cond is true |
| Assert.assertEquals(actual, expected) | objects/values aren’t equal |
| Assert.assertNotEquals(a, b) | objects/values are equal |
| Assert.assertNull(obj) | obj is not null |
| Assert.assertNotNull(obj) | obj is null |
| Assert.fail("message") | unconditionally marks test as failed |

5.2 Soft Asserts

```javascript

                import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;

public class SoftAssertExample {

  @Test
  public void multipleChecks() {

    SoftAssert soft = new SoftAssert();

    soft.assertTrue(isElementVisible(), "Element should be visible");

    soft.assertEquals(getTitle(), "Dashboard", "Title mismatch");

    soft.assertNotNull(getUser(), "User should be logged in");

    // Reports any failures above; must call or test passes regardless

    soft.assertAll();

  }

  private boolean isElementVisible() { /*…*/ return false; }

  private String getTitle()             { /*…*/ return "";  }

  private Object getUser()              { /*…*/ return null; }

}
```

Even if the first two soft.assert… fail, the method continues through all checks.

soft.assertAll() at the end aggregates and reports all failures at once.

**6. Commonly Used TestNG Assert Methods**

```javascript

                // org.testng.Assert

Assert.assertTrue(condition, "Optional failure message");

Assert.assertFalse(condition, "Optional failure message");

Assert.assertEquals(actual, expected, "Optional failure message");

Assert.assertNotEquals(actual, expected, "Optional failure message");

Assert.assertNull(object, "Optional failure message");

Assert.assertNotNull(object, "Optional failure message");

Assert.fail("Explicit failure with message");

// org.testng.asserts.SoftAssert

SoftAssert soft = new SoftAssert();

soft.assertTrue(...);

soft.assertFalse(...);

soft.assertEquals(...);

soft.assertAll();
```

By choosing **hard asserts** for critical checkpoints (fail fast) and **soft asserts** when you want to collect multiple verification results in one test, you can make your TestNG suites both robust and informative.
