Search code examples
groovyannotationsjunit4

Groovy Attribute should have type 'java.lang.Integer', but found type 'java.lang.Object'


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

Solution

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