Search code examples
intellij-ideagroovy

Groovy/IntelliJ: groovy.lang.MissingPropertyException: No such property


I'm a groovy beginner and using IntelliJ trying to build a sample project and some tests in spock for the class sampleHelper. Running my test for the method createToken results in the following error:

groovy.lang.MissingPropertyException: No such property: sampleConstants for class: com.tutorial.sampleHelper

My understanding is that classes in the same package do not need to be imported. I must be making a silly mistake but not sure where I'm going wrong. Any pointers are appreciated.

Clicking on sampleConstants.TOKEN from the class does go to the correct file.

I have the following folder structure:

└── sample-project
    ├── pom.xml
    ├── resources
    ├── src --> set as sources root
    │   └── com
    │       └── tutorial
    │           ├── sampleConstants
    │           └── sampleHelper
    └── tests --> set as tests root
        └── com
            └── tutorial
                └── sampleHelper

sampleHelper.groovy:

package com.tutorial

public class sampleHelper implements Serializable {
    Boolean enabled

    public sampleHelper(config) {
        this.enabled = config.enabled
    }

    def createToken() {
        def token = enabled ? "abc" : sampleConstants.TOKEN
        return token
    }
}

sampleConstants.groovy

package com.tutorial

public class sampleConstants {
    static final TOKEN = "cde"
}

Solution

  • Classes in Java (and Groovy) traditionally start with a capital letter.

    The Groovy parser sometimes relies on this tradition, and if you use classes with lower-case letters, it struggles to see what you mean and instead looks for a field or variable with that name.

    Always using a capital letter for class names (and the files that contain them) is always a good idea, as it saves you from things like this (which look like the world has gone mad) 😉