1package test.inject; 2 3import org.testng.ITestResult; 4import org.testng.SkipException; 5import org.testng.annotations.AfterMethod; 6import org.testng.annotations.BeforeClass; 7import org.testng.annotations.BeforeMethod; 8import org.testng.annotations.Test; 9 10import java.lang.reflect.Method; 11 12public class InjectAfterMethodWithTestResultSampleTest { 13 static int m_success; 14 15 @Test 16 public void pass() {} 17 18 @Test 19 public void fail() { 20 throw new RuntimeException(); 21 } 22 23 @Test 24 public void skip() { 25 throw new SkipException("Skipped"); 26 } 27 28 @BeforeClass 29 public void init() { 30 m_success = 3; 31 } 32 33 @BeforeMethod 34 public void before(Method m, ITestResult r) { 35 System.out.println("Before result: " + r); 36 } 37 38 @AfterMethod 39 public void after(Method m, ITestResult r) { 40 String name = m.getName(); 41 if (("pass".equals(name) && r.getStatus() == ITestResult.SUCCESS) 42 || ("fail".equals(name) && r.getStatus() == ITestResult.FAILURE) 43 || ("skip".equals(name) && r.getStatus() == ITestResult.SKIP)) { 44 m_success--; 45 } 46 } 47} 48