I have some TestNG code, where I am passing a Test annotation parameter called timeOut = TESTNG_TEST_TIMEOUT
.
@Test(description = "Tests something.", groups = { "regression" }, timeOut = TESTNG_TEST_TIMEOUT, enabled = true)
And in my TestBase class I have this member:
public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);
When I use the above line of code, I get a 'attribute value must be constant' error in Eclipse.
But, if I simply define the member like so, it works:
public final static long TESTNG_TEST_TIMEOUT = 300000;
Is the use of TimeUnit not a constant?
A constant expression is an expression that yields a primitive type or a String, and whose value can be evaluated at compile time to a literal. The expression must evaluate without throwing an exception, and it must be composed of only the following: Primitive and String literals.
if the compiler expects an "Array Initializer" to be passed to the Annotation, declaring a compile-time constant like private static final String[] AB = { ... }; should do. it's understood that Annotation processing happens before the actual compilation, but then the error message is not accurate.
We can also explicitly specify the attributes in a @Test annotation. Test attributes are the test specific, and they are specified at the right next to the @Test annotation.
This
public final static long TESTNG_TEST_TIMEOUT = 300000;
is a constant variable, a type of constant expression.
This
public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);
is not.
Annotation members expect constant expressions (and a few other things like enums and Class
literals).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With