[ 
https://issues.apache.org/jira/browse/GROOVY-6764?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King resolved GROOVY-6764.
-------------------------------
       Resolution: Fixed
         Assignee: Paul King
    Fix Version/s: 2.4.8

Proposed PR merged. Thanks for spotting the issue Jeff.

> Problem Referring To Statically Imported Constants
> --------------------------------------------------
>
>                 Key: GROOVY-6764
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6764
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.1.9, 2.4.0-rc-1
>            Reporter: Jeff Brown
>            Assignee: Paul King
>             Fix For: 2.4.8
>
>         Attachments: constantsquestion.zip
>
>
> It appears that if a class implements an interface which declares constants, 
> Groovy code which statically imports the constant from the class (not the 
> interface) doesn't work.  I think this is supposed to work.  It does work 
> from Java.  See the attached app in constantsquestion.zip which includes the 
> following code.  Extract the .zip and run "./gradlew test" to see that the 
> Java test passes and the Groovy test fails.
> {code:borderStyle=solid|title=src/main/java/com/demo/Constants.java}
> package com.demo;
> public interface Constants {
>     int ANSWER = 42;
> }
> {code}
> {code:borderStyle=solid|title=src/main/java/com/demo/Helper.java}
> package com.demo;
> public class Helper implements Constants {
> }
> {code}
> {code:borderStyle=solid|title=test/java/com/demo/JavaHelperTests.java}
> package com.demo;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import org.junit.runners.JUnit4;
> import static org.junit.Assert.assertEquals;
> import static com.demo.Helper.ANSWER;
> @RunWith(JUnit4.class)
> public class JavaHelperTests {
>     @Test
>     public void testAccessingConstant() {
>         // this test passes...
>         assertEquals(42, ANSWER);
>     }
> }
> {code}
> {code:borderStyle=solid|title=test/groovy/com/demo/GroovyHelperSpec.groovy}
> package com.demo
> import spock.lang.Specification
> import static com.demo.Helper.ANSWER
> class GroovyHelperSpec extends Specification {
>     void 'test referring to statically imported constant'() {
>         // this test fails...
>         expect:
>         42 == ANSWER
>     }
> }
> {code}
> The error message from the Groovy spec is:
> {noformat}
> groovy.lang.MissingPropertyException: No such property: ANSWER for class: 
> com.demo.GroovyHelperSpec
>     at com.demo.GroovyHelperSpec.test referring to statically imported 
> constant(GroovyHelperSpec.groovy:10)
> {noformat}
> Note that everything works fine from Groovy if the constant is imported from 
> the interface instead of from the class which implements the interface.
> Is this a bug?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to