Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java why this error: 'attribute value must be constant'?

Tags:

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?

like image 956
djangofan Avatar asked Jan 19 '15 19:01

djangofan


People also ask

What is a constant expression in Java?

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.

How do you use an array constant in annotation?

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.

What is the annotation used to define attributes for an element?

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.


1 Answers

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).

like image 148
Sotirios Delimanolis Avatar answered Sep 19 '22 13:09

Sotirios Delimanolis