Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

@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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...