Java 批注属性Test.enabled的值必须是常量表达式
基本上,我想使用Context类的一个常量布尔属性,我已经通过反射对该属性进行了更改,以便可以在testNG类中为testNG方法动态设置@annotated enabled。TestNG类有一个与上下文相同的静态final属性。如果堆栈旧,则解散\u TEST\u CASES\u。我已经为TestNG类及其方法粘贴了下面的代码我的最终目标是切换启用值,或基本上根据上下文禁用测试(如果是旧环境或新环境)Java 批注属性Test.enabled的值必须是常量表达式,java,reflection,testng,Java,Reflection,Testng,基本上,我想使用Context类的一个常量布尔属性,我已经通过反射对该属性进行了更改,以便可以在testNG类中为testNG方法动态设置@annotated enabled。TestNG类有一个与上下文相同的静态final属性。如果堆栈旧,则解散\u TEST\u CASES\u。我已经为TestNG类及其方法粘贴了下面的代码我的最终目标是切换启用值,或基本上根据上下文禁用测试(如果是旧环境或新环境) package com.abc.api.core.context; import
package com.abc.api.core.context;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Context {
public static final boolean DISBLE_TEST_CASES_IF_OLD_STACK = getConstantReflection();
public static boolean getConstantReflection()
{
System.out.println(DISBLE_TEST_CASES_IF_OLD_STACK);
try {
setEnableFlagBasedOnStackForTestCases();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Context.DISBLE_TEST_CASES_IF_OLD_STACK);
try {
final Field fld = Context.class.getDeclaredField("DISBLE_TEST_CASES_IF_OLD_STACK");
return (Boolean) fld.get( null );
} catch (NoSuchFieldException e) {
return (Boolean) null;
} catch (IllegalAccessException e) {
return (Boolean) null;
}
}
private static void setEnableFlagBasedOnStackForTestCases() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Field f = Context.class.getDeclaredField("DISBLE_TEST_CASES_IF_OLD_STACK");
f.setAccessible(true);
//'modifiers' - it is a field of a class called 'Field'. Make it accessible and remove
//'final' modifier for our 'CONSTANT' field
Field modifiersField = Field.class.getDeclaredField( "modifiers" );
modifiersField.setAccessible( true );
modifiersField.setInt( f, f.getModifiers() & ~Modifier.FINAL );
if (TestcaseContext.getContext().equalsIgnoreCase(Context.OLD_STACK)) {
f.setBoolean(null, false);
}else {
f.setBoolean(null, true);
}
}
}
TESTNG类和方法示例:
package com.abc.api.test.tests.TestA;
import com.abc.api.core.context.Context;
public class TestA extends TestCommon {
private static final boolean ENABLE_DISABLE = Context.DISBLE_TEST_CASES_IF_OLD_STACK;
/**
*
*/
@BeforeTest
void setPropertiesFile() {
......
}
/**
* PATCH Positive Test Cases
*
*/
@Test(priority = 11, enabled=ENABLE_DISABLE)
public void testPositive1() {
......
}
}
为了有选择地禁用testcases的最终目标,您可以使用TestNgs IANotationTransformer来控制enabled标志。它将在每个@Test注释的方法之前运行,并可以控制其执行 例如
如果旧堆栈不是常量变量,则您的
解散测试案例。您将无法在注释中使用它,但反射实际上不是在修改静态终值,它实际上是类的常量属性。因此,我使用了反射的方法。编译器可以直接复制常量变量值,并且在运行时不会真正读取它们,所以即使它编译了,这也不起作用。看,这正是我最后要做的。然而,由于某种原因,当我用listener类注释TestNG类时,没有调用listener。我不得不在测试套件中通过xml使用监听器,这有点让人扫兴,因为我只想将这个监听器应用于这个特定的类。此外,我发现我可以只使用组,并根据需要排除和包含它们。
public class DemoTransformer implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod)
{
if (condition) {
annotation.setEnabled(false/true);
}
}