简体   繁体   English

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

[英]The value for annotation attribute Test.enabled must be a constant expression

Basically, I wanted to use a constant boolean attribute of Context class which I have changed via reflection so that I can dynamically set the @annotated enabled for a testNG method in a TestNG class. 基本上,我想使用通过反射更改的Context类的常量布尔属性,以便可以在TestNG类中动态设置为testNG方法启用的@annotated。 The TestNG class has a static final attribute which is the same as the Context.DISBLE_TEST_CASES_IF_OLD_STACK. TestNG类的静态最终属性与Context.DISBLE_TEST_CASES_IF_OLD_STACK相同。 I have pasted the code below for the TestNG class and its method. 我已经为TestNG类及其方法粘贴了以下代码。 The end goal for me is to toggle the enabled value or basically disable the test based on the the context if its old environment or new environment 我的最终目标是切换启用的值,或者基本基于上下文禁用测试(如果测试的环境是旧环境还是新环境)

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

To the end goal of selectively disabling testcases, you can use TestNgs IAnnotationTransformer to control the enabled flag. 为了最终有选择地禁用测试用例,可以使用TestNgs IAnnotationTransformer来控制启用的标志。 It will be run before each @Test annotated method and can control it's execution. 它将在每个@Test带注释的方法之前运行,并且可以控制其执行。

eg. 例如。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在@ test.enabled属性中提供运行时值 - How to give runtime value in @test.enabled attribute CsvBindByPosition:注释属性的值必须是常量表达式 - CsvBindByPosition : value for annotation attribute must be a constant expression annotation属性的值必须是常量表达式 - value for the annotation attribute must be constant expression 注释属性RequestMapping.value的值必须是常量表达式 - The value for annotation attribute RequestMapping.value must be a constant expression 注释属性Query.value的值必须是一个常量表达式 - The value for annotation attribute Query.value must be a constant expression 注释属性Min.value的值必须是常量表达式 - The value for annotation attribute Min.value must be a constant expression 注释属性GetMapping.produces的值必须是一个常量表达式 - The value for annotation attribute GetMapping.produces must be a constant expression 为什么注释属性Rest.rootUrl的值必须是常量表达式? - Why The value for annotation attribute Rest.rootUrl must be a constant expression? Java注解,属性值必须是常量 - Java annotation, Attribute value must be constant 通过函数构建端点值-注释属性X的值必须为常量表达式 - Building endpoint values from a function - The value for annotation attribute X must be a constant expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM