0
votes

I'm trying to work out how I get a Groovy test script to import a Java class during the testing phase ...

Specifically I want to use JavaFXThreadingRule: .java file from here (or rather here and so included in my Java test source path) and then import it in my Groovy test script to use as an annotation.

The Groovy test script path is src\test\ft\groovy\core\testscript.groovy.
The .java file is src\test\ft\java\core\JavaFXThreadingRule.java.
The package line I've used in both is "package core;"
My "sourceSets" clause in build.gradle looks like this:

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
    }

    test {
        java {
            srcDirs = ['src/test/ft/java' ]
        }
        groovy {
            srcDirs = ['src/test/ft/groovy', 'src/test/ut/groovy']
        }
    }
}

Interestingly the build.gradle output shows that the compileTestJava task is run before the compileTestGroovy task ... and yet I get

unable to resolve class core.JavaFXThreadingRule @ line 18, column 1. import core.JavaFXThreadingRule ^

NB I also tried "import JavaFXThreadingRule" - same result.

In addition to just wanting to resolve the problem I'm also wondering how Gradle decides what order to do the tasks compileTestJava and compileTestGroovy... and whether I shouldn't perhaps make my compileTestGroovy explicitly dependent on compileTestJava...

1
Stick the Java file under your Groovy path - tim_yates
@Ori Dar that class file (and 3 others it produces) are actually under build\classes\java\test (this is a Windoze machine). - mike rodent
@tim_yates... Ha... thanks, works. I tried that initially, but I think I made the mistake of not including the package statement. Now I put it in it works. Still seems slightly puzzling that the compileTestGroovy task can't pick up and use classes from the compileTestJava task. Sigh: I have much to learn about Gradle. - mike rodent
Think this is a groovy compiler thing, not necessarily a gradle thing. Glad it works - tim_yates

1 Answers

0
votes

Thanks to Tim Yates I found the "workaround" of putting this Java file in with the Groovy ones... but this answer gave me another clue, and I then changed my build.gradle to be like this:

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
    }
    test {
        groovy {
            srcDirs = ['src/test/ft/groovy', 'src/test/ut/groovy', 'src/test/ft/java' ]
        }
    }
}

... works ... no doubt obvious to old Gradle hands.

That question and answer referenced above were talking about the app code classes (and Gradle tasks). Unless some Gradle expert can say otherwise I'm assuming that the Groovy test compile task and Java test compile task are completely separate and can't "see" one another's classes...