Skip to content

Commit 4e3b8bd

Browse files
committed
#5 use IInvokedMethodListener to circumvent NPE caused in @beforeMethod
1 parent 06d585d commit 4e3b8bd

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/main/java/com/aventstack/extentreports/testng/listener/ExtentITestListenerAdapter.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.aventstack.extentreports.testng.listener;
22

3-
import org.testng.ITestContext;
4-
import org.testng.ITestListener;
5-
import org.testng.ITestResult;
3+
import org.testng.*;
64

75
import com.aventstack.extentreports.AnalysisStrategy;
86
import com.aventstack.extentreports.service.ExtentService;
97
import com.aventstack.extentreports.service.ExtentTestManager;
108

11-
public class ExtentITestListenerAdapter implements ITestListener {
9+
public class ExtentITestListenerAdapter implements ITestListener, IInvokedMethodListener {
10+
11+
private Boolean createdMethodBeforeInvocation = false;
1212

1313
@Override
1414
public synchronized void onStart(ITestContext context) {
@@ -22,7 +22,8 @@ public synchronized void onFinish(ITestContext context) {
2222

2323
@Override
2424
public synchronized void onTestStart(ITestResult result) {
25-
ExtentTestManager.createMethod(result);
25+
if (!createdMethodBeforeInvocation)
26+
ExtentTestManager.createMethod(result);
2627
}
2728

2829
@Override
@@ -44,4 +45,17 @@ public synchronized void onTestSkipped(ITestResult result) {
4445
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
4546
}
4647

48+
@Override
49+
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
50+
if (method.getTestMethod().isBeforeMethodConfiguration()) {
51+
createdMethodBeforeInvocation = true;
52+
ExtentTestManager.createMethod(testResult);
53+
}
54+
}
55+
56+
@Override
57+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
58+
59+
}
60+
4761
}

src/main/java/com/aventstack/extentreports/testng/listener/ExtentITestListenerClassAdapter.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.aventstack.extentreports.testng.listener;
22

3-
import org.testng.ITestContext;
4-
import org.testng.ITestListener;
5-
import org.testng.ITestResult;
3+
import org.testng.*;
64

75
import com.aventstack.extentreports.AnalysisStrategy;
86
import com.aventstack.extentreports.service.ExtentService;
97
import com.aventstack.extentreports.service.ExtentTestManager;
108

11-
public class ExtentITestListenerClassAdapter implements ITestListener {
9+
public class ExtentITestListenerClassAdapter implements ITestListener, IInvokedMethodListener {
10+
11+
private Boolean createdMethodBeforeInvocation = false;
1212

1313
@Override
1414
public synchronized void onStart(ITestContext context) {
@@ -22,7 +22,8 @@ public synchronized void onFinish(ITestContext context) {
2222

2323
@Override
2424
public synchronized void onTestStart(ITestResult result) {
25-
ExtentTestManager.createMethod(result, true);
25+
if (!createdMethodBeforeInvocation)
26+
ExtentTestManager.createMethod(result, true);
2627
}
2728

2829
@Override
@@ -44,4 +45,16 @@ public synchronized void onTestSkipped(ITestResult result) {
4445
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
4546
}
4647

48+
@Override
49+
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
50+
if (method.getTestMethod().isBeforeMethodConfiguration()) {
51+
createdMethodBeforeInvocation = true;
52+
ExtentTestManager.createMethod(testResult, true);
53+
}
54+
}
55+
56+
@Override
57+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
58+
59+
}
4760
}

0 commit comments

Comments
 (0)