1 package test.inject; 2 3 import org.testng.ITestResult; 4 import org.testng.SkipException; 5 import org.testng.annotations.AfterMethod; 6 import org.testng.annotations.BeforeClass; 7 import org.testng.annotations.BeforeMethod; 8 import org.testng.annotations.Test; 9 10 import java.lang.reflect.Method; 11 12 public class InjectAfterMethodWithTestResultSampleTest { 13 static int m_success; 14 15 @Test pass()16 public void pass() {} 17 18 @Test fail()19 public void fail() { 20 throw new RuntimeException(); 21 } 22 23 @Test skip()24 public void skip() { 25 throw new SkipException("Skipped"); 26 } 27 28 @BeforeClass init()29 public void init() { 30 m_success = 3; 31 } 32 33 @BeforeMethod before(Method m, ITestResult r)34 public void before(Method m, ITestResult r) { 35 System.out.println("Before result: " + r); 36 } 37 38 @AfterMethod after(Method m, ITestResult r)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