注释属性Test.enabled的值必须是常量表达式

时间:2017-08-28 20:26:30

标签: java reflection testng

基本上,我想使用Context类的常量布尔属性,我已经通过反射更改了这个属性,以便我可以在TestNG类中为testNG方法动态设置@annotated。 TestNG类具有静态final属性,该属性与Context.DISBLE_TEST_CASES_IF_OLD_STACK相同。我已经为TestNG类及其方法粘贴了下面的代码。 我的最终目标是切换启用的值,或者如果旧环境或新环境

,则基于上下文基本禁用测试
 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 CLASS AND METHOD示例:

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() {
        ......
    }
}

1 个答案:

答案 0 :(得分:2)

为了有选择地禁用测试用例的最终目标,您可以使用TestNgs IAnnotationTransformer来控制已启用的标志。它将在每个@Test注释方法之前运行,并可以控制它的执行。

例如。

public class DemoTransformer implements IAnnotationTransformer {
  public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod)
  {
    if (condition) {
        annotation.setEnabled(false/true);
  }
}