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

I have some TestNG code, where I am passing a Test annotation parameter called timeOut = TESTNG_TEST_TIMEOUT . And in my TestBase class I have this member: When I use the above line of code, I get a 'attribute value must be constant' error in Eclipse.

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?

1

1 Answer

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

4

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaXFoY2aEeYCOoqVmopGrrm7Dx7JkraCZqHqmvtGoqWaZpKm%2Fqq7UrZxmrpGhwqZ5zK6qrWWSmnqku82sq5qmpA%3D%3D

 Share!