mercredi 13 décembre 2017

Get annotation value dynamically, not hard coding the class/method name to get the annotation value

I have some tests running on testNG, which will have test data type annotated on the test method, i am using a common a method to read the test data based on the annotation value in test methods. Problem here is, i can not specify the class or method name to read the annotation as there will be different/multiple test methods from different classes that will be reading the test data. So i am looking for a way to read the annotation dynamically from this common method, based on which test method is seeking for the test data.

Please have a look on the below code snippet for more clarity.

public class MyClass1 {

    @Test
    @testDataParam(testDataType="excel")
    public void test1() {

        DataTable dataTable = new DataTable();
        dataTable.getValue();
        //Some test that reads from specified test data type
    }

}

public class MyClass2 {

    @Test
    @testDataParam(testDataType="json")
    public void test2() {

        DataTable dataTable = new DataTable();
        dataTable.getValue();
        //Some test that reads from specified test data type
    }

}

public class GetTestData {

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface testDataParam {
        String testDataType();
    }

}

public class DataTable {

    public void setDataTable() {

        // get annotation from test methods, test methods will be different for
        // different test, so i can not mention specific class or method here to read
        // annotation.

        // if test data type is excel, read data from excel
        // if test data type is json, read data from json
    }

    public String getValue() {

        // return value from specified data type excel/json
        return "";
    }
}





Aucun commentaire:

Enregistrer un commentaire