2

I am trying to change the test parameter description on run time. All the examples I have seen refers to changing annotation value at class level. But, I want to do it at method level.

My sample code is as below:

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.Method;

public class TestClass {

    @BeforeMethod
    public void beforeMethod(Method method){
        Test annotation = method.getAnnotation(Test.class);
        // I want to change description to "some modified description"
    }

    @Test(description = "some description")
    public void test(){
        System.out.println("in test");
    }
}
VIBHOR GOYAL
  • 473
  • 1
  • 6
  • 22
  • 1
    Question: why do you want to change the description at runtime instead of rewriting it in the code? – Turing85 Jan 10 '20 at 16:34

1 Answers1

0

Annotation parameters cannot be changed because they have to be known at compile-time.

You could use an interface with a getter and setter instead of an annotation if you need to change it.

With TestNG, you can implement ITest and overwrite the method getTestName() like this answer describes.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Can I add an annotation by myself at runtime then? I want to add it during runtime so that I can change description when using multiple data using data provider. – VIBHOR GOYAL Jan 10 '20 at 16:39
  • No, annotations must be known at compile time. – dan1st Jan 10 '20 at 18:00
  • 1
    nah, nothing needs to be known at compile time, just doing it is a very poor and bad idea but: https://stackoverflow.com/questions/57733914/how-i-can-change-method-annotations-value-in-runtime https://stackoverflow.com/questions/14268981/modify-a-class-definitions-annotation-string-parameter-at-runtime?noredirect=1&lq=1 – GotoFinal Jan 13 '20 at 09:39