Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

TestNG Framework

  • What is TestNG Framework?
  • Advantages and Disadvantages of TestNG:
  • Difference between TestNG And Junit framework
  • What is TestNG Annotations?
  • Install TestNG In Eclipse & IntelliJ?
  •  Hierarchy In TestNG Annotations
  • TestNG’s prioritization
  • TestNG Dependent
  • Reporter Class in TestNG
  • TestNG Reports
  • Assertions in TestNG
  • TestNG Groups
  • TestNG Parameters
  •  Cross-Browser Testing in TestNG
  •  Parallel Testing in TestNG
  • Data Providers
  • TestNG Listeners
  • Rerunning failed tests in TestNG
View Categories
  • Home
  • Tutorials
  • TestNG
  • TestNG Framework
  • Assertions in TestNG

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:

Assert.assertTrue(condition);

Assert.assertEquals(actual, expected);

Assert.assertNotNull(object);

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

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

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:

@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 #

MethodFails 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 #

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

// 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.

testng framework
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
TestNG ReportsTestNG Groups
Table of Contents
  • 5.1 Hard Asserts
  • 5.2 Soft Asserts
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105