1 package test;
2 
3 import org.testng.IInvokedMethod;
4 import org.testng.IInvokedMethodListener;
5 import org.testng.ITestResult;
6 
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.List;
10 
11 // TODO replace other test IInvokedMethodListener by this one
12 public class InvokedMethodNameListener implements IInvokedMethodListener {
13 
14   private final List<String> invokedMethodNames = new ArrayList<>();
15   private final List<String> failedMethodNames = new ArrayList<>();
16   private final List<String> skippedMethodNames = new ArrayList<>();
17   private final List<String> succeedMethodNames = new ArrayList<>();
18 
19   @Override
beforeInvocation(IInvokedMethod method, ITestResult testResult)20   public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
21     invokedMethodNames.add(method.getTestMethod().getConstructorOrMethod().getName());
22   }
23 
24   @Override
afterInvocation(IInvokedMethod method, ITestResult testResult)25   public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
26     switch (testResult.getStatus()) {
27       case ITestResult.FAILURE:
28         failedMethodNames.add(method.getTestMethod().getConstructorOrMethod().getName());
29         break;
30       case ITestResult.SKIP:
31         skippedMethodNames.add(method.getTestMethod().getConstructorOrMethod().getName());
32         break;
33       case ITestResult.SUCCESS:
34         succeedMethodNames.add(method.getTestMethod().getConstructorOrMethod().getName());
35         break;
36     }
37   }
38 
getInvokedMethodNames()39   public List<String> getInvokedMethodNames() {
40     return Collections.unmodifiableList(invokedMethodNames);
41   }
42 
getFailedMethodNames()43   public List<String> getFailedMethodNames() {
44     return failedMethodNames;
45   }
46 
getSkippedMethodNames()47   public List<String> getSkippedMethodNames() {
48     return skippedMethodNames;
49   }
50 
getSucceedMethodNames()51   public List<String> getSucceedMethodNames() {
52     return succeedMethodNames;
53   }
54 }
55