Road to override TestNg listener methods

In this post I will show you how to create custom testng listener  class and override its methods.
Create java class like I created below “MyListner” and inherit “TestListenerAdapter” testing class.

package com.test;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class MyListner extends TestListenerAdapter {
       
     @Override
     public void onTestFailure(ITestResult result) {
        System.out.println(result.getName() + " Test method failed\n");
     }

     @Override
     public void onTestSkipped(ITestResult result) {
        System.out.println(result.getName() + " Test method skipped\n");
     }

     @Override
     public void onTestSuccess(ITestResult result) {
        System.out.println(result.getName() + " Test method success\n");
     } 
 
Create testng class and use above listener like below
package com.test;

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

@Listeners ({MyListner.class})
public class SampleTest {

    @Test
    public void testFirst() {
        Assert.assertTrue(true);
    }

    @Test
    public void testSecond() {
        Assert.assertTrue(false);
    }

    @Test(dependsOnMethods = { "testSecond" })
    public void testThird() {
        Assert.assertTrue(true);
    }
}

Run above test you will see below message in console:


1 comment:

  1. Hi,
    Your posts are really helpful..
    Can you help me in auto running failed test's in Testng..

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution