Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.1.9, 2.4.0-rc-1
-
None
-
None
Description
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.
package com.demo; public interface Constants { int ANSWER = 42; }
package com.demo; public class Helper implements Constants { }
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); } }
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 } }
The error message from the Groovy spec is:
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)
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?
Attachments
Attachments
Issue Links
- links to