Assertions in testng

Assertions related methods are put in org.testng.Assert class. We have below methods to assert in out tests
  • Assert.assertEquals
  • Assert.assertFalse
  • Assert.assertTrue
  • Assert.assertNull
  • Assert.assertNotNull
  • Assert.assertSame
  • Assert.assertNotSame
  • Assert.fail
Here is the sample example that explains TestNG Assertions.
 
package org.softpost;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGAssertions {

    @Test
    public void testAssertions(){

        String x=null;

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

        Assert.assertTrue(2==2);
        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
        Assert.assertSame(t1,t2);

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

    }
}

Web development and Automation testing

solutions delivered!!