1

I have the following JUnit test:

public class JavaTest {

    final int value = 2;

    @Test
    @Repeat(times = value)
    public void test() {
        fail("Not yet implemented");
    }
}

The @Repeat annotation comes from easytest-core, and the exact definition is here.

When I compile this as java source everything builds (and runs) fine. When I compile the exact same thing as groovy source, I get:

Groovy:Attribute 'times' should have type 'java.lang.Integer'; but found type 'java.lang.Object' in @org.easetech.easytest.annotation.Repeat GroovyTest.groovy

After searching the internets, I found a few similar discussions on SO and jira.codehaus, but those deal with String - GString problems, so the solutions do not work for me.

How can I fix this?

Updates:

  • java.version=1.7.0_76
  • groovy.version=2.3.7
11
  • Have you tried final Integer in place of final int? Commented Apr 15, 2015 at 22:14
  • @tim_yates I have now. :p Groovy reports the same problem. Java now reports "Type mismatch: cannot convert from Integer to int". Commented Apr 15, 2015 at 22:19
  • where does it report that? with the final... or with the times=? Commented Apr 15, 2015 at 22:27
  • @cfrick The error is on the @Repeat(times = value) line. Commented Apr 16, 2015 at 4:34
  • 1
    Just as an experiment you could try adding @CompileStatic to the class. Commented Apr 16, 2015 at 13:30

1 Answer 1

1

Think you're bumping into the fact groovyc doesn't treat final variables as inline constants like javac does

I tried changing your int variable like this:

final Integer value = Integer.valueOf(2).intValue()

which prevents the variable from being treated as an inline constant. After that change I get a compile error from the @Repeat annotation:

Expected Integer.valueOf(2).intValue() to be an inline constant

It looks like there's some acknowledgement of the inconsistency here in a Groovy JIRA: https://issues.apache.org/jira/browse/GROOVY-1628

There's also some further discussion here in this SO thread: Does it make sense to mark variable as final in groovy?

It doesn't look like you're going to be able to get groovy to match the Java behavior for this scenario.

Sign up to request clarification or add additional context in comments.

1 Comment

Bummer. That jira ticket says something is fixed in version 2.5.0, but Maven Central says the last released is 2.4.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.