Best Testng code snippet using org.testng.Interface IConfigurationListener.beforeConfiguration
Source:CarinaListenerChain.java
...26import com.nordstrom.automation.testng.ListenerChain;27public class CarinaListenerChain extends ListenerChain {28 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());29 // The IConfigurationListener interface has two methods with the same meaning:30 // 1. void beforeConfiguration(ITestResult tr)31 // 2. void beforeConfiguration(ITestResult tr, ITestNGMethod tm)32 //33 // The only difference is that the version with two arguments also passes the actual test method34 // for which a configuration method is going to be invoked, but the first version don't.35 //36 // The Zebrunner agent expects the second version of the method to be invoked.37 // This is basically necessary to correlate a test method with corresponding to it @BeforeMethod and @AfterMethod methods.38 //39 // The problem is that Nordstrom's ListenerChain class doesn't decorate this method.40 // As a result, the Zebrunner agent misses invocations of @BeforeMethod and @AfterMethod methods.41 @Override42 public void beforeConfiguration(ITestResult tr, ITestNGMethod tm) {43 try {44 Field configListenersField = this.getClass().getSuperclass().getDeclaredField("configListeners");45 configListenersField.setAccessible(true);46 List<IConfigurationListener> listeners = (List<IConfigurationListener>) configListenersField.get(this);47 invokeListeners(tr, tm, listeners);48 } catch (NoSuchFieldException | IllegalAccessException e) {49 LOGGER.error("Could not invoke configuration listeners.", e);50 }51 }52 private void invokeListeners(ITestResult tr, ITestNGMethod tm, List<IConfigurationListener> listeners) {53 synchronized (listeners) {54 for (IConfigurationListener configListener : Lists.reverse(listeners)) {55 configListener.beforeConfiguration(tr, tm);56 }57 }58 }59}
Source:TestNGConfigurationListener.java
...41 * Invoked before a configuration method is invoked. 42 * 43 * Note: This method is only invoked for TestNG 6.2 or higher.44 */45 void beforeConfiguration(ITestResult tr);46}...
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!