0

When implementing IAnnotationTransfer interface in TestNG, there is a parameter called annotation which is the annotation that would get read from the test class. Now, there are few methods that are straightforward whereas various methods of annotation that I am not able to understand (such as getAttributes). Can someone point me to example usages (descriptions) of these methods so that I can come to know how to use some of these methods.

Specifically, what getAttributes return?

I tried to use it (CustomAttribute[] cs = annotation.getAttributes();) but I am getting nothing in cs variable.

All the methods in the IAnnotation interface can be accessed below:

https://javadoc.io/doc/org.testng/testng/7.1.0/org/testng/annotations/ITestAnnotation.html

Vijay Gupta
  • 21
  • 1
  • 7

1 Answers1

0

Below is a sample that shows how to work with CustomAttribute. The other methods are all self explanatory.

The test class that uses CustomAttribute would look like below:

import org.testng.Reporter;
import org.testng.annotations.CustomAttribute;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Optional;

public class SampleTestClass {

    @Test(attributes = {
            @CustomAttribute(name = "functionality", values = {"login", "oauth"}),
            @CustomAttribute(name = "flavor", values = {"bvt", "regression"})
    })
    public void customAttributeEnabledTestMethod() {
        System.err.println("Printing the custom attributes from test method");
        CustomAttribute[] attributes = Optional
                .ofNullable(
                        Reporter.getCurrentTestResult().getMethod().getAttributes())
                .orElse(new CustomAttribute[]{});
        Arrays.stream(attributes).forEach(attribute -> {
            System.err.println("Attribute Name : " + attribute.name());
            System.err.println("Attribute values : " + Arrays.toString(attribute.values()));
        });
    }

    @Test
    public void plainTestMethod() {
        System.err.println("Hello world again");
    }
}

The custom annotation transformer that alters value for this:

import org.testng.IAnnotationTransformer;
import org.testng.annotations.CustomAttribute;
import org.testng.annotations.ITestAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;

public class DemoAnnotationTransformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

        CustomAttribute[] attributes = annotation.getAttributes();
        //We need this filtering logic because there's currently a bug in TestNG 7.1.0
        //which causes the annotation transfomer to be invoked multiple times.
        //This should be resolved in TestNG v7.2.0
        //The defect to refer to : https://github.com/cbeust/testng/issues/2312
        Optional<CustomAttribute> found = Arrays.stream(attributes)
                .filter(each -> each.name().equalsIgnoreCase("supported-browsers"))
                .findAny();

        if (found.isPresent()) {
            return;
        }

        int size = attributes.length + 1;
        CustomAttribute[] copied = new CustomAttribute[size];
        System.arraycopy(attributes, 0, copied, 0, attributes.length);
        copied[attributes.length] = new CustomAttribute() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return CustomAttribute.class;
            }

            @Override
            public String name() {
                return "supported-browsers";
            }

            @Override
            public String[] values() {
                return new String[]{"firefox", "chrome"};
            }
        };
        annotation.setAttributes(copied);
    }
}

Here's how the suite xml looks like :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="qn_62287783_suite" parallel="false" configfailurepolicy="continue">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.qn62287783.DemoAnnotationTransformer"/>
    </listeners>
    <test name="qn_62287783_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn62287783.SampleTestClass"/>
        </classes>
    </test>
</suite>

Here's how the output looks like:

Printing the custom attributes from test method
Attribute Name : functionality
Attribute values : [login, oauth]
Attribute Name : flavor
Attribute values : [bvt, regression]
Attribute Name : supported-browsers
Attribute values : [firefox, chrome]


Hello world again

PASSED: customAttributeEnabledTestMethod
PASSED: plainTestMethod

===============================================
    qn_62287783_test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
qn_62287783_suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66