import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.testng.TestNG;
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface Condition {
Class<? extends TestCondition> test();
interface TestCondition {
boolean test();
}
class TestNGFutureVersion implements TestCondition {
@Override
public boolean test() {
String file = TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile();
return new File(file).getAbsolutePath().contains("7.5.0");
}
}
}
import com.rationaleemotions.conditions.Condition.TestCondition;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.InstanceCreator;
public class ConditionBasedTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
Condition condition = testMethod.getAnnotation(Condition.class);
if (condition != null) {
Class<? extends TestCondition> predicate = condition.test();
TestCondition instance = InstanceCreator.newInstance(predicate);
annotation.setEnabled(instance.test());
}
}
}
import com.rationaleemotions.conditions.Condition.TestNGFutureVersion;
import org.testng.annotations.Test;
public class ExampleTestClass {
@Test
@Condition(test = TestNGFutureVersion.class)
public void testMethod() {
System.err.println("This can run only in TestNG 7.5.0");
}
}
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/6c23de1f-e550-47f2-b2bb-4b4eac255f11n%40googlegroups.com.