Skip to content

Commit 75bb7ee

Browse files
committed
fix #2 - npe when onTestStart was not called for ITestListener
1 parent 3672a17 commit 75bb7ee

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

src/main/java/com/aventstack/extentreports/service/ExtentTestManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static synchronized ExtentTest createTest(ITestResult result, ExtentTest
9696
return test;
9797
}
9898

99-
public static synchronized void log(ITestResult result) {
99+
public static synchronized void log(ITestResult result, Boolean createTestAsChild) {
100100
String msg = "Test ";
101101
Status status = Status.PASS;
102102
switch (result.getStatus()) {
@@ -112,11 +112,18 @@ public static synchronized void log(ITestResult result) {
112112
msg += "passed";
113113
break;
114114
}
115+
if (ExtentTestManager.getTest(result) == null) {
116+
ExtentTestManager.createMethod(result, createTestAsChild);
117+
}
115118
if (result.getThrowable() != null) {
116119
ExtentTestManager.getTest(result).log(status, result.getThrowable());
117120
return;
118121
}
119122
ExtentTestManager.getTest(result).log(status, msg);
120123
}
124+
125+
public static synchronized void log(ITestResult result) {
126+
log(result, false);
127+
}
121128

122129
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public synchronized void onTestStart(ITestResult result) {
2727

2828
@Override
2929
public synchronized void onTestSuccess(ITestResult result) {
30-
ExtentTestManager.log(result);
30+
ExtentTestManager.log(result, true);
3131
}
3232

3333
@Override
3434
public synchronized void onTestFailure(ITestResult result) {
35-
ExtentTestManager.log(result);
35+
ExtentTestManager.log(result, true);
3636
}
3737

3838
@Override
3939
public synchronized void onTestSkipped(ITestResult result) {
40-
ExtentTestManager.log(result);
40+
ExtentTestManager.log(result, true);
4141
}
4242

4343
@Override

src/test/java/com/aventstack/extentreports/adapter/testlistener/Class1Tests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.aventstack.extentreports.adapter.testlistener;
22

33
import org.testng.Assert;
4+
import org.testng.SkipException;
5+
import org.testng.annotations.BeforeSuite;
46
import org.testng.annotations.Listeners;
57
import org.testng.annotations.Test;
68

79
import com.aventstack.extentreports.testng.listener.ExtentITestListenerAdapter;
810

911
@Listeners({ExtentITestListenerAdapter.class})
1012
public class Class1Tests extends BaseDataProvider {
13+
14+
@BeforeSuite
15+
public void BeforeSuite() {
16+
System.out.println("Before Suite");
17+
throw new SkipException("Skip Suite");
18+
}
1119

1220
@Test(dataProvider = "Authentication", groups = { "functest", "checkintest" })
1321
public void passClass1(String user, String password) {
@@ -19,4 +27,10 @@ public void failClass1(String user, String password) {
1927
Assert.assertTrue(false);
2028
}
2129

30+
@Test
31+
public void SkipClass1() {
32+
System.out.println("Inside Skip Test");
33+
throw new SkipException("Skip Class1 Test skipped");
34+
}
35+
2236
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.aventstack.extentreports.adapter.testlistener;
2+
3+
import org.testng.Assert;
4+
import org.testng.SkipException;
5+
import org.testng.annotations.BeforeSuite;
6+
import org.testng.annotations.Listeners;
7+
import org.testng.annotations.Test;
8+
9+
import com.aventstack.extentreports.testng.listener.ExtentITestListenerAdapter;
10+
11+
@Listeners({ExtentITestListenerAdapter.class})
12+
public class Class3Tests extends BaseDataProvider {
13+
14+
@BeforeSuite
15+
public void beforeSuite() {
16+
throw new SkipException("intentional skip");
17+
}
18+
19+
@Test
20+
public void passClass2() {
21+
Assert.assertTrue(true);
22+
}
23+
24+
@Test(groups = { "functest", "checkintest" })
25+
public void failClass2() {
26+
Assert.assertTrue(false);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)