Cas
Cas

Reputation: 758

MissingPropertyException even though it's imported

I'm working on some integration tests in Groovy. There are a few hundred tests and I've been tasked with removing hardcoded ID's in favor of a single file with constants. For about 500 of the files, everything works as expected (Thanks, grep and sed): hardcoded ID's have been replaced with a constant:

old

package com.example.package.tests;

class SomeTest extends BaseGroovyTest { 

    @Test
    public void testSomething() {           
        def merchantId = "1234"

        ...
    }
}

new

package com.example.package.tests;
import com.example.package.util.ConstantMerchants

class SomeTest extends BaseGroovyTest { 

    @Test
    public void testSomething() {           
        def merchantId = ConstantMerchants.MERCHANT_A

        ...
    }
}

ConstantMerchants

package com.example.package.util;

public final class ConstantMerchants {

    public static final String MERCHANT_A = "1234";
    public static final String MERCHANT_B = "1111";
    public static final String MERCHANT_C = "2222";
    public static final String MERCHANT_D = "3333";
    ...

    private ConstantMerchants() { }

}

However, while some files were changed successfully (so the hardcoded ID is replaced with a reference to ConstantMerchants and ConstantMerchants is imported), their tests can't run due to the error:

groovy.lang.MissingPropertyException: No such property: ConstantMerchants for class: com.example.package.tests.SomeTest

I've checked multiple times, and the only modifications to the files are the addition of an import station for ConstantMerchants, and the replacement of a string ID with a ConstantMerchants counterpart.

Removing the import statement and then having eclipse add it by pressing Ctrl+Shift+O (Organize imports) results in the same file I had before removing the import statement, and the error still occurring.

Any help in resolving this issue would be very much appreciated!

Upvotes: 1

Views: 309

Answers (2)

Cas
Cas

Reputation: 758

Apparently some class that was used by a class that was used by a class (...) that was used by my test referenced ConstantMerchants but didn't properly import it.

For some reason the compiler thought this was the fault of the class at the end of the usage chain so it kept saying my tests were missing references while it was actually some util class a few layers deep. Adding the import to that file fixed the issue.

I finally found this by grepping for files that contain ConstantMerchants but not the import statement. If anyone is interested, my grep command was:

grep -L -e "import .*ConstantMerchants" $(grep -E -f /mnt/c/Data/search_replaced.txt * -rclH)

Where search_replaced.txt contains a few patterns that match the replaced IDs.

Upvotes: 1

Rotem
Rotem

Reputation: 1379

You mentioned that you have many tests running and some of them fails. That makes me think that there is a dependency problem of your tests, means maybe some tests are not allowed to have the class ConstantMerchants in their import correctly. Do the following:

  • Try to change the class ConstantMerchants position where all the tests have an access to that class.

Another thing you should check is your IDE synchronization:

  • Delete the build directory.
  • Rebuild the project.

Check if that helped.

No, then check for any other synchronization issues of the IDE.

If it didn't work, try another thing: Check your building tool if it has some limitations or any Environment variable it needs for running in the best way.

And do the follows:

  • Delete the build directory
  • Perform a synchronization to the project libraries.
  • Rerun the project. Most of the time it works great and can help eliminate that error.

I hope it helps.

Upvotes: 1

Related Questions