ITestResult is throwing an exception | Selenium Forum
H
Harshad W Posted on 31/10/2020

Hi,

I am getting the following exception when I use the ITestResult interface in the @BeforeMethod of TestBase class. What could I to get this error rsolved? Is this a problem with TestNG 7.0. I came across this thread. https://github.com/cbeust/testng-eclipse/issues/307. It's quite olde though.

Here's the code snippet:

package suitea;


import org.testng.annotations.Test;

import com.aventstack.extentreports.Status;

import reports.ExtentManager;
import testbase.TestBase;

public class TestA extends TestBase {
	
	
	@Test
	public void testA() throws InterruptedException
	{
		System.out.println("Starting Test A");
		test.log(Status.INFO, "A Test Started");
		Thread.sleep(3000);
		System.out.println("Ending Test A");
		test.log(Status.INFO, "A Test Ended");
	}
}
package testbase;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;

import reports.ExtentManager;

public class TestBase {

	
	public ExtentReports rep;
	public ExtentTest test;
	
	@BeforeMethod
	public void init(ITestResult result)
	{
		
		rep = ExtentManager.getReports();
		System.out.println(result.getMethod().getMethodName().toUpperCase());
		test = rep.createTest(result.getMethod().getMethodName().toUpperCase());
		
	}
	
	@AfterMethod
	public void quit()
	{
		rep.flush();
	}
}




Error:

[TestNG] Running:
  C:\Users\harshad.w\AppData\Local\Temp\testng-eclipse--998853898\testng-customsuite.xml

Sat Oct 31 19-07-50 IST 2020//screenshots
D:\Eclipse\eclipse\TestNGLiveProject//reports//Sat Oct 31 19-07-50 IST 2020//screenshots
D:\Eclipse\eclipse\TestNGLiveProject//reports//Sat Oct 31 19-07-50 IST 2020
java.lang.RuntimeException
	at org.testng.internal.TestResult.toString(TestResult.java:251)
	at org.testng.internal.TestResult.toString(TestResult.java:236)
	at org.testng.reporters.TestHTMLReporter.generateTable(TestHTMLReporter.java:114)
	at org.testng.reporters.TestHTMLReporter.generateLog(TestHTMLReporter.java:299)
	at org.testng.reporters.TestHTMLReporter.onFinish(TestHTMLReporter.java:40)
	at org.testng.TestRunner.fireEvent(TestRunner.java:1239)
	at org.testng.TestRunner.afterRun(TestRunner.java:1030)
	at org.testng.TestRunner.run(TestRunner.java:636)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
	at org.testng.SuiteRunner.run(SuiteRunner.java:268)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.run(TestNG.java:1064)
	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)

A
Ashish Thakur Replied on 04/11/2020

Please try to use it in listener class... not in before test


H
Harshad W Replied on 06/11/2020

Where do I include it? I created a listener class and them like:

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;

import reports.ExtentManager;

public class MyTestNGListener extends TestBase implements ITestListener {
public ExtentReports rep;
public ExtentTest test;

public void onFinish(ITestContext arg0) {
// TODO Auto-generated method stub

}

public void onStart(ITestContext arg0) {
// TODO Auto-generated method stub

}

public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
// TODO Auto-generated method stub

}

public void onTestFailure(ITestResult arg0) {
// TODO Auto-generated method stub

}

public void onTestSkipped(ITestResult arg0) {
// TODO Auto-generated method stub

}

public void onTestStart(ITestResult result) {
rep = ExtentManager.getReports();
System.out.println(result.getMethod().getMethodName().toUpperCase());
test = rep.createTest(result.getMethod().getMethodName().toUpperCase());

}

-------------------------

public class TestBase {


public ExtentReports rep;
public ExtentTest test;


@AfterMethod
public void quit()
{
rep.flush();
}

public void log(String msg)
{
test.log(Status.INFO, msg);
}
}

I removed the @BeforeMethod annotation from the TestBase class and included it in the above method of Listener. 

I am geting the attached error when executing it. 


A
Ashish Thakur Replied on 08/11/2020

Why are you extending TestBase in Listener?


H
Harshad W Replied on 14/11/2020

Sorry, I messed up the things. I created the listener class and included the following code in onStart method of listener. It's now ceating the reports. 

This is the change I made to get it to work. Is it fine to have this in the TestSuccess method of Listener. Also, I have attached the reports I get.

When I run the suite, I get all reports created in one folder. 

public void onTestSuccess(ITestResult result) {
rep = ExtentManager.getReports();
System.out.println(result.getMethod().getMethodName().toUpperCase());
test = rep.createTest(result.getMethod().getMethodName().toUpperCase());
String startMessage = "Starting " + result.getName();
String endMessage = "Ending " + result.getName();
test.log(Status.INFO, startMessage);
test.log(Status.INFO, endMessage);
rep.flush();
}

 


H
Harshad W Replied on 27/11/2020

Is the above code good to go with?

 


A
Ashish Thakur Replied on 28/11/2020

Can you plese zip your project and send it across?


H
Harshad W Replied on 28/11/2020

Attached is the project I am planning to use for the next modules. I have completed first video on TestNG Live project. If the attached project is good to use for the next modules/chapters, then please let me know.

 


A
Ashish Thakur Replied on 02/12/2020

Harshad... Your listener class implementation is incorrect. Listener should not be extending base class

There are many other tech errors as well.

Please try to correct each. Watch video once and make paralelly


H
Harshad W Replied on 03/12/2020

Hi Ashish,

No, I have changed the code. Please have a look at my preious comment. I have removed the TestBase class. Can you please look at the attached .zip project and let me know? It's not showing any errors now. Attaching the project again.

 

 


A
Ashish Thakur Replied on 03/12/2020

Its running fine at my end

Please let me know version of TestNG in eclipse which you are using


M
Replied on 03/12/2020

Thanks for confirming it. I am using TestNG 7.0.0 version.

So, should I continue with that code?

 


A
Ashish Thakur Replied on 04/12/2020

Yes


Related Posts