Class TestNGParameterListener
- java.lang.Object
-
- org.confng.testng.TestNGParameterListener
-
- All Implemented Interfaces:
ConfNGListener,org.testng.IInvokedMethodListener,org.testng.ISuiteListener,org.testng.ITestListener,org.testng.ITestNGListener
public class TestNGParameterListener extends Object implements org.testng.ITestListener, org.testng.IInvokedMethodListener, org.testng.ISuiteListener, org.testng.ITestNGListener, ConfNGListener
TestNG listener that automatically captures test parameters and makes them available to ConfNG as a configuration source.This listener is automatically loaded via TestNG's service loader mechanism when ConfNG is on the classpath. It captures parameters from:
- Suite parameters (from testng.xml)
- Test parameters (from testng.xml)
- Method parameters (from @Parameters annotation)
Parameters are made available with the following precedence:
- Method-level parameters (highest priority)
- Test-level parameters
- Suite-level parameters (lowest priority)
This listener implements
ConfNGListenerwith priority 0, allowing custom listeners to execute before (negative priority) or after (positive priority) the configuration loading process.Automatic Configuration Loading
When the test suite starts, this listener automatically loads configuration files in the following order (first loaded = highest precedence):
- Config Files - Auto-loads standard config files (HIGHEST PRECEDENCE):
- config.properties, config.json, config.yaml, config.toml
- File type is auto-detected based on extension
- Files that don't exist are silently skipped
- Environment-Specific Configuration - Loads configuration files
specific to the current environment (MEDIUM PRECEDENCE):
- If a suite parameter named "environment" or "env" is present in testng.xml, loads {environment}.properties, {environment}.json, {environment}.yaml, {environment}.toml
- If no environment parameter is found, attempts auto-detection by checking the APP_ENV, ENVIRONMENT, or ENV environment variables
- If no environment is detected, defaults to "local"
- Global/Common Configuration - Loads configuration files that apply
to all environments (LOWEST PRECEDENCE, provides defaults):
- global.properties, global.json, global.yaml, global.toml
- common.properties, common.json, common.yaml, common.toml
This loading order ensures that config.* files have the highest precedence, followed by environment-specific values, and finally global values as defaults. This allows you to define common defaults in global configuration files, override them per environment, and have project-specific overrides in config.* files.
Example Configuration Structure
config.json (highest precedence):
{ "browser": "chrome", "headless": "true" }uat.json (medium precedence):
{ "api.url": "https://uat-api.example.com", "api.timeout": "60", "database.host": "uat-db.example.com" }global.json (lowest precedence, provides defaults):
{ "api.timeout": "30", "retry.count": "3", "log.level": "INFO" }Result for UAT environment: browser=chrome (from config.json), headless=true (from config.json), api.timeout=60 (from uat.json, overrides global), retry.count=3 (from global.json), log.level=INFO (from global.json), api.url and database.host from uat.json
Example testng.xml
<suite name="My Test Suite"> <parameter name="environment" value="uat"/> <test name="My Test"> <classes> <class name="com.example.MyTest"/> </classes> </test> </suite>- Since:
- 1.0
- Author:
- Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
- See Also:
TestNGParameterSource,ConfNG.loadGlobalConfig(),ConfNG.loadConfigForEnvironment(String),ConfNG.autoLoadConfig()
-
-
Constructor Summary
Constructors Constructor Description TestNGParameterListener()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterInvocation(org.testng.IInvokedMethod method, org.testng.ITestResult testResult)Called after a method is invoked.voidbeforeInvocation(org.testng.IInvokedMethod method, org.testng.ITestResult testResult)Called before a method is invoked.static TestNGParameterSourcegetParameterSource()Gets the current TestNG parameter source.intgetPriority()Returns the priority of this listener in the listener chain.voidonFinish(org.testng.ISuite suite)Called when a suite finishes (TestNG ISuiteListener method).voidonFinish(org.testng.ITestContext context)Called when a test context finishes (ITestListener).voidonStart(org.testng.ISuite suite)Called when a suite starts.voidonStart(org.testng.ITestContext context)Called when a test context starts (ITestListener).voidonSuiteFinish(org.testng.ISuite suite)Implementation of ConfNGListener.onSuiteFinish - called by the listener chain.voidonSuiteStart(org.testng.ISuite suite)Implementation of ConfNGListener.onSuiteStart - this is called by the listener chain.voidonTestFailedButWithinSuccessPercentage(org.testng.ITestResult result)Called when a test fails but is within the success percentage.voidonTestFailedWithTimeout(org.testng.ITestResult result)Called when a test fails with timeout.voidonTestFailure(org.testng.ITestResult result)Called when a test fails.voidonTestSkipped(org.testng.ITestResult result)Called when a test is skipped.voidonTestStart(org.testng.ITestResult result)Called when a test starts.voidonTestSuccess(org.testng.ITestResult result)Called when a test succeeds.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.confng.testng.ConfNGListener
afterMethodInvocation, beforeMethodInvocation
-
-
-
-
Method Detail
-
getPriority
public int getPriority()
Returns the priority of this listener in the listener chain. Priority 0 is the default for TestNGParameterListener, allowing custom listeners to execute before (negative numbers) or after (positive numbers).- Specified by:
getPriorityin interfaceConfNGListener- Returns:
- the priority value (can be negative, zero, or positive)
-
onStart
public void onStart(org.testng.ISuite suite)
Called when a suite starts. Captures suite-level parameters and automatically loads configuration files with auto-detection of file types.Auto-loads the following configuration files (if they exist):
- config.properties, config.json, config.yaml, config.toml - HIGHEST PRECEDENCE
- Environment-specific configuration files ({environment}.properties, {environment}.json, etc.)
- Global/common configuration files (global.properties, global.json, etc.) - LOWER PRECEDENCE (provides defaults)
This ensures that config.* files have the highest precedence, followed by environment-specific values, and finally global values as defaults.
- Specified by:
onStartin interfaceorg.testng.ISuiteListener
-
onSuiteStart
public void onSuiteStart(org.testng.ISuite suite)
Implementation of ConfNGListener.onSuiteStart - this is called by the listener chain. This method contains the actual configuration loading logic.- Specified by:
onSuiteStartin interfaceConfNGListener- Parameters:
suite- the test suite
-
onTestStart
public void onTestStart(org.testng.ITestResult result)
Called when a test starts. Captures test-level parameters. This method satisfies both ITestListener.onTestStart and ConfNGListener.onTestStart.When called by TestNG, delegates to the listener chain. When called by the chain, executes the actual parameter capture logic.
- Specified by:
onTestStartin interfaceConfNGListener- Specified by:
onTestStartin interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
beforeInvocation
public void beforeInvocation(org.testng.IInvokedMethod method, org.testng.ITestResult testResult)Called before a method is invoked. Captures method-level parameters. This method satisfies both IInvokedMethodListener and ConfNGListener.- Specified by:
beforeInvocationin interfaceConfNGListener- Specified by:
beforeInvocationin interfaceorg.testng.IInvokedMethodListener- Parameters:
method- the invoked methodtestResult- the test result
-
afterInvocation
public void afterInvocation(org.testng.IInvokedMethod method, org.testng.ITestResult testResult)Called after a method is invoked. Cleanup method-level parameters. This method satisfies both IInvokedMethodListener and ConfNGListener.- Specified by:
afterInvocationin interfaceConfNGListener- Specified by:
afterInvocationin interfaceorg.testng.IInvokedMethodListener- Parameters:
method- the invoked methodtestResult- the test result
-
onTestSuccess
public void onTestSuccess(org.testng.ITestResult result)
Called when a test succeeds. Cleanup test-level parameters. This method satisfies both ITestListener.onTestSuccess and ConfNGListener.onTestSuccess.- Specified by:
onTestSuccessin interfaceConfNGListener- Specified by:
onTestSuccessin interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
onTestFailure
public void onTestFailure(org.testng.ITestResult result)
Called when a test fails. Cleanup test-level parameters. This method satisfies both ITestListener.onTestFailure and ConfNGListener.onTestFailure.- Specified by:
onTestFailurein interfaceConfNGListener- Specified by:
onTestFailurein interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
onTestSkipped
public void onTestSkipped(org.testng.ITestResult result)
Called when a test is skipped. Cleanup test-level parameters. This method satisfies both ITestListener.onTestSkipped and ConfNGListener.onTestSkipped.- Specified by:
onTestSkippedin interfaceConfNGListener- Specified by:
onTestSkippedin interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
onTestFailedButWithinSuccessPercentage
public void onTestFailedButWithinSuccessPercentage(org.testng.ITestResult result)
Called when a test fails but is within the success percentage. This method satisfies both ITestListener and ConfNGListener.- Specified by:
onTestFailedButWithinSuccessPercentagein interfaceConfNGListener- Specified by:
onTestFailedButWithinSuccessPercentagein interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
onTestFailedWithTimeout
public void onTestFailedWithTimeout(org.testng.ITestResult result)
Called when a test fails with timeout. This method satisfies both ITestListener and ConfNGListener.- Specified by:
onTestFailedWithTimeoutin interfaceConfNGListener- Specified by:
onTestFailedWithTimeoutin interfaceorg.testng.ITestListener- Parameters:
result- the test result
-
onStart
public void onStart(org.testng.ITestContext context)
Called when a test context starts (ITestListener). Delegates to the listener chain.- Specified by:
onStartin interfaceConfNGListener- Specified by:
onStartin interfaceorg.testng.ITestListener- Parameters:
context- the test context
-
onFinish
public void onFinish(org.testng.ITestContext context)
Called when a test context finishes (ITestListener). Note: This is different from onFinish(ISuite) which is for ISuiteListener. Delegates to the listener chain.- Specified by:
onFinishin interfaceConfNGListener- Specified by:
onFinishin interfaceorg.testng.ITestListener- Parameters:
context- the test context
-
onFinish
public void onFinish(org.testng.ISuite suite)
Called when a suite finishes (TestNG ISuiteListener method). Delegates to the listener chain which will call onSuiteFinish on all listeners.- Specified by:
onFinishin interfaceorg.testng.ISuiteListener
-
onSuiteFinish
public void onSuiteFinish(org.testng.ISuite suite)
Implementation of ConfNGListener.onSuiteFinish - called by the listener chain. Cleanup suite-level parameters.- Specified by:
onSuiteFinishin interfaceConfNGListener- Parameters:
suite- the test suite
-
getParameterSource
public static TestNGParameterSource getParameterSource()
Gets the current TestNG parameter source. This method is primarily for testing purposes.- Returns:
- the current parameter source, or null if not initialized
-
-