06
Apr

class scope annotations processing with TestNG

Step 1: your class scoped annotation:
————————————–
package com.CustomTestNG.Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TesterInfo{

public enum PRIORITY {LOW, MEDIUM, HIGH}

PRIORITY priority() default PRIORITY.LOW;
String tester() default “Sheetal”;

}

Step 2: your TestNG Interface custom implementation:
————————————————–
package com.CustomTestNG.Annotation;

import java.lang.annotation.Annotation;

import org.testng.IClassListener;
import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class MyTestNGAnnotation implements ITestListener, IClassListener {

private int sCount = 0;

@Override
public void onTestStart(ITestResult tr) {
Annotation an = tr.getMethod().getConstructorOrMethod().getMethod().getAnnotation(TesterInfo.class);
TesterInfo obj = (TesterInfo) an;
if (obj.tester().equals(“Sheetal”)) {
sCount++;
}
}

@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub

}

@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub

}

@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub

}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub

}

@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub

}

@Override
public void onFinish(ITestContext context) {
System.out.println(“Count of runs for sheetal ” + sCount);
}

@Override
public void onBeforeClass(ITestClass testClass) {
Class c = testClass.getRealClass();
Annotation an = c.getAnnotation(TesterInfo.class);
TesterInfo obj = (TesterInfo)an;
if(obj.tester().equals(“Sheetal”)) {
sCount–;
}
}

@Override
public void onAfterClass(ITestClass testClass) {
// TODO Auto-generated method stub

}

}

Step 3: your TestNG Script:
————————–
package com.CustomTestNG.Annotation;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.CustomTestNG.Annotation.TesterInfo.PRIORITY;

@Listeners(MyTestNGAnnotation.class)
@TesterInfo(tester=”Sheetal”, priority=PRIORITY.LOW)
public class TestNGScript {

@Test
@TesterInfo(tester=”Sheetal”, priority=PRIORITY.LOW)
public void s() {
System.out.println(“Sheetal”);
}

}

OUTPUT:
——-

[RemoteTestNG] detected TestNG version 6.14.3
Sheetal
Count of runs for sheetal 0
PASSED: s

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================