Class TestNGListenerChain


  • public class TestNGListenerChain
    extends Object
    Manages the chain of ConfNG listeners and executes them in priority order.

    This class discovers and manages custom listeners that implement the ConfNGListener interface. Listeners are executed in priority order, where lower priority values execute first.

    The chain includes:

    • Custom pre-processing listeners (negative priority)
    • TestNGParameterListener (priority 0)
    • Custom post-processing listeners (positive priority)

    Listener Discovery:

    Custom listeners are discovered using Java's ServiceLoader mechanism. To register a custom listener:

    1. Implement the ConfNGListener interface
    2. Create a file: META-INF/services/org.confng.testng.ConfNGListener
    3. Add your listener class name(s) to the file

    Example:

     // File: META-INF/services/org.confng.testng.ConfNGListener
     com.example.CustomPreListener
     com.example.CustomPostListener
     
    Since:
    1.0.3
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    See Also:
    ConfNGListener, TestNGParameterListener
    • Method Detail

      • getInstance

        public static TestNGListenerChain getInstance()
        Gets the singleton instance of the listener chain.
        Returns:
        the listener chain instance
      • initialize

        public void initialize​(ConfNGListener coreListener)
        Initializes the listener chain by discovering and loading all registered listeners. This method is thread-safe and will only initialize once.
        Parameters:
        coreListener - the core TestNGParameterListener instance to include in the chain
      • executeSuiteStart

        public void executeSuiteStart​(org.testng.ISuite suite)
        Executes all listeners' onSuiteStart method in priority order.
        Parameters:
        suite - the test suite
      • executeSuiteFinish

        public void executeSuiteFinish​(org.testng.ISuite suite)
        Executes all listeners' onSuiteFinish method in priority order.
        Parameters:
        suite - the test suite
      • executeTestStart

        public void executeTestStart​(org.testng.ITestResult result)
        Executes all listeners' onTestStart method in priority order.
        Parameters:
        result - the test result
      • executeTestSuccess

        public void executeTestSuccess​(org.testng.ITestResult result)
        Executes all listeners' onTestSuccess method in priority order.
        Parameters:
        result - the test result
      • executeTestFailure

        public void executeTestFailure​(org.testng.ITestResult result)
        Executes all listeners' onTestFailure method in priority order.
        Parameters:
        result - the test result
      • executeTestSkipped

        public void executeTestSkipped​(org.testng.ITestResult result)
        Executes all listeners' onTestSkipped method in priority order.
        Parameters:
        result - the test result
      • executeBeforeMethodInvocation

        public void executeBeforeMethodInvocation​(org.testng.ITestContext testContext,
                                                  String methodName)
        Executes all listeners' beforeMethodInvocation method in priority order.
        Parameters:
        testContext - the test context
        methodName - the method name
      • executeAfterMethodInvocation

        public void executeAfterMethodInvocation​(org.testng.ITestContext testContext,
                                                 String methodName)
        Executes all listeners' afterMethodInvocation method in priority order.
        Parameters:
        testContext - the test context
        methodName - the method name
      • executeTestFailedButWithinSuccessPercentage

        public void executeTestFailedButWithinSuccessPercentage​(org.testng.ITestResult result)
        Executes all listeners' onTestFailedButWithinSuccessPercentage method in priority order.
        Parameters:
        result - the test result
      • executeTestFailedWithTimeout

        public void executeTestFailedWithTimeout​(org.testng.ITestResult result)
        Executes all listeners' onTestFailedWithTimeout method in priority order.
        Parameters:
        result - the test result
      • executeTestContextStart

        public void executeTestContextStart​(org.testng.ITestContext context)
        Executes all listeners' onStart method (ITestListener) in priority order.
        Parameters:
        context - the test context
      • executeTestContextFinish

        public void executeTestContextFinish​(org.testng.ITestContext context)
        Executes all listeners' onFinish method (ITestListener) in priority order.
        Parameters:
        context - the test context
      • executeBeforeInvocation

        public void executeBeforeInvocation​(org.testng.IInvokedMethod method,
                                            org.testng.ITestResult testResult)
        Executes all listeners' beforeInvocation method (IInvokedMethodListener) in priority order.
        Parameters:
        method - the invoked method
        testResult - the test result
      • executeAfterInvocation

        public void executeAfterInvocation​(org.testng.IInvokedMethod method,
                                           org.testng.ITestResult testResult)
        Executes all listeners' afterInvocation method (IInvokedMethodListener) in priority order.
        Parameters:
        method - the invoked method
        testResult - the test result
      • getSharedContext

        public Map<String,​Object> getSharedContext()
        Gets the shared context map that can be used to pass data between listeners.
        Returns:
        the shared context map
      • getListeners

        public List<ConfNGListener> getListeners()
        Gets all registered listeners in priority order.
        Returns:
        an unmodifiable list of listeners