importorg.testng.annotations.Test;importorg.testng.annotations.BeforeMethod;importorg.testng.annotations.AfterMethod;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeTest;importorg.testng.annotations.AfterTest;importorg.testng.annotations.BeforeSuite;importorg.testng.annotations.AfterSuite;publicclassTestngAnnotation{// test case 1@TestpublicvoidtestCase1(){System.out.println("in test case 1");}// test case 2@TestpublicvoidtestCase2(){System.out.println("in test case 2");}@BeforeMethodpublicvoidbeforeMethod(){System.out.println("in beforeMethod");}@AfterMethodpublicvoidafterMethod(){System.out.println("in afterMethod");}@BeforeClasspublicvoidbeforeClass(){System.out.println("in beforeClass");}@AfterClasspublicvoidafterClass(){System.out.println("in afterClass");}@BeforeTestpublicvoidbeforeTest(){System.out.println("in beforeTest");}@AfterTestpublicvoidafterTest(){System.out.println("in afterTest");}@BeforeSuitepublicvoidbeforeSuite(){System.out.println("in beforeSuite");}@AfterSuitepublicvoidafterSuite(){System.out.println("in afterSuite");}}
<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suitename="Suite1"><testname="test1"><classes><classname="TestngAnnotation"/></classes></test></suite>
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================