更改方法注释参数值

问题描述 投票:1回答:1

我正在尝试在运行时更改测试参数说明。我看到的所有示例都涉及在类级别更改注释值。但是,我想在方法级别执行此操作。

我的示例代码如下:

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");
    }
}
java reflection testng
1个回答
0
投票

注释参数不能更改,因为必须在编译时知道它们。

如果需要更改它,可以使用带有getter和setter的接口,而不是使用注释。

使用TestNG,您可以实现ITest并像getTestName()所描述的那样覆盖方法this answer

© www.soinside.com 2019 - 2024. All rights reserved.