While running testng test Parallel=methods on extent reports throws Null Pointer exception. PLEASE HELP
Its been 3 days i have did lot of reasearch on finding a solution to generate extent reports when running testng test parallel= methods but no luck yet on top of that i have very close deadline to demo my project to the client. I BADLY NEED SOMEONE HELP TO FIND A SOLUTION WHOEVER IS AVAILABLE I REALLY APPRECIATE.
Note : When i run the test parallel=classes it works fine but since i have requirement to run test only on parallel=methods i can't go with parallel=classes.
Here are list of classes i have in my project
================= BaseTest =================
public class BaseTest extends TestListenerAdapter { public ThreadLocal<ExtentTest> parentTest = new ThreadLocal<ExtentTest>(); public ThreadLocal<ExtentTest> test = new ThreadLocal<ExtentTest>();
@BeforeClass
public synchronized void beforeClass() throws Exception {
ExtentTest parent = ExtentTestManager.createTest(getClass().getName());
parentTest.set(parent);
//throw new Exception("Failed ******* ");
}
@BeforeMethod
public synchronized void beforeMethod(Method method) {
//String value = String.valueOf(parentTest.get().toString());
ExtentTest child = parentTest.get().createNode(method.getName());
test.set(child);
}
@AfterMethod
public synchronized void afterMethod(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE)
test.get().log(Status.FAIL,result.getThrowable());
else if (result.getStatus() == ITestResult.SKIP)
test.get().skip(result.getThrowable());
else
test.get().pass("Test passed");
ExtentManager.getExtent().flush();
}
}
=========================================
================= ExtentManager =================
public class ExtentManager { public static Properties prop = new Properties();
public static ExtentReports extent;
public static String filePath = System.getProperty("user.dir") + "/target/ExtentReports.html";
public ExtentManager() throws IOException {
prop.load(new FileInputStream("config.properties"));
}
public synchronized static ExtentReports getExtent() {
if (extent == null) {
extent = new ExtentReports();
extent.attachReporter(getHtmlReporter());
/* if (System.getenv("ExtentX").equalsIgnoreCase("true")) { extent.attachReporter(getExtentXReporter()); }*/ extent.setSystemInfo("Selenium Java Version", "2.53.0"); extent.setSystemInfo("Environment", "Prod"); extent.setSystemInfo("AppiumVersion", "4.0.0");
}
return extent;
}
private static ExtentHtmlReporter getHtmlReporter() {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath);
// make the charts visible on report open
htmlReporter.config().setChartVisibilityOnOpen(true);
// report title
//String documentTitle = prop.getProperty("documentTitle", "aventstack - Extent");
htmlReporter.config().setDocumentTitle("AppiumTestDistribution");
htmlReporter.config().setReportName("ExtentReports");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
return htmlReporter;
}
private static ExtentXReporter getExtentXReporter() {
String host = prop.getProperty("MONGODB_SERVER");
Integer port = Integer.parseInt(prop.getProperty("MONGODB_PORT"));
ExtentXReporter extentx = new ExtentXReporter(host, port);
// project name
String projectName = prop.getProperty("projectName", "ExtentReports");
extentx.config().setProjectName(projectName);
// report or build name
String reportName = prop.getProperty("reportName", "ExtentReports");
extentx.config().setReportName(reportName);
// server URL
// ! must provide this to be able to upload snapshots
return extentx;
}
}
=========================================
================= ExtentTestManager =================
public class ExtentTestManager { // new public static Properties prop = new Properties();
public static ThreadLocal<ExtentTest> extentTest = new ThreadLocal<ExtentTest>();
public static ExtentReports extent = ExtentManager.getExtent();
public synchronized static ExtentTest getTest() {
return extentTest.get();
}
public synchronized static ExtentTest createTest(String name, String description,
String deviceId) {
ExtentTest test = extent.createTest(name, description);
if (deviceId != null && !deviceId.isEmpty())
// test.assignCategory(deviceId);
extentTest.set(test);
return getTest();
}
public synchronized static ExtentTest createTest(String name, String description) {
return createTest(name, description, String.valueOf(Thread.currentThread().getId()));
}
public synchronized static ExtentTest createTest(String name) {
return createTest(name, "Sample Test");
}
}
=========================================
================= SampleTest =================
public class SampleTest extends BaseTest {
@Test
public void testApp(){
System.out.println("ThreadName: " + Thread.currentThread().getName() + Thread.currentThread().getStackTrace()[1].getClassName());
test.get().pass("This is test one");
}
@Test
public void testApp1(){
System.out.println("ThreadName: " + Thread.currentThread().getName() + Thread.currentThread().getStackTrace()[1].getClassName());
test.get().assignCategory("ONE");
}
@Test
public void testApp2(){
System.out.println("ThreadName: " + Thread.currentThread().getName() + Thread.currentThread().getStackTrace()[1].getClassName());
test.get().assignCategory("TWO");
AssertJUnit.assertTrue(false);
}
}
=========================================
================= testng.xml =================
<classes>
<class name="com.test.sample.SampleTest" />
</classes>
</test> <!-- Test -->
=========================================
================ Exception =====================

=========================================
Thanks Vinit
@anshooarora , Can you please help, this is holding my project deadline.
Thanks Vinit