groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Closed] (GROOVY-6764) Problem Referring To Statically Imported Constants
Date Wed, 01 Feb 2017 23:19:05 GMT

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

Paul King closed GROOVY-6764.
-----------------------------

> 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.15#6346)

Mime
View raw message