Assertions in TestNG

Assertions related methods are put in  org.testng.Assert class. We have below methods to assert in out tests

  1. Assert.assertEquals
  2. Assert.assertFalse
  3. Assert.assertTrue
  4. Assert.assertNull
  5. Assert.assertNotNull
  6. Assert.assertSame
  7. Assert.assertNotSame

Here is the sample example that explains TestNG Assertions.

package org.softpost;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGAssertions {

    public void testAssertions(){

        String x=null;

        Assert.assertEquals(2,2,"equality test");
        Assert.assertNotEquals(2, 3, "Inequality test");

        Assert.assertFalse(2 == 1);
        Assert.assertNull(x, "Checking if x is null");
        Assert.assertNotNull("Watson","Checking that watson is not null");

        TestNGAssertions t1= new TestNGAssertions();
        TestNGAssertions t2= t1;
        //Below will pass as t1 and t2 refer to same objects

        TestNGAssertions t3= new TestNGAssertions();
        TestNGAssertions t4= new TestNGAssertions();
        //Below will fail as t3 and t4 refer to different objects not same
        Assert.assertSame(t3,t4);"Fail the test");


You may also like...