Reputation: 63
I have a project where I've split up my logic into multiple gradle files and written them into the ext{} block. Here's a simplified version of what I'm doing to illustrate my issue:
build.gradle
:
plugins {
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
buildSrc/src/main/resources/testJson.json
:
{
"dogs": [
{
"name": "chip",
"id": 1
},
{
"name": "spot",
"id": 2
}
]
}
buildSrc/src/main/scripts/parseDogs.gradle
:
import groovy.json.JsonSlurper
configure(project.rootProject) {
ext.parseDogs = { ->
def json = new JsonSlurper().parseText(file('src/main/resources/testJson.json').text)
def names = []
json.dogs.each { dog ->
names.add(dog.name)
}
names
}
}
Here's what I have related to my test:
buildSrc/build.gradle
:
plugins {
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.spockframework:spock-core:2.0-groovy-2.5'
testImplementation 'org.codehaus.groovy:groovy-all:2.5.4'
}
test {
useJUnitPlatform()
}
buildSrc/src/test/groovy/ExampleTest.groovy
:
import spock.lang.Specification
class ExampleTest extends Specification{
// passes
def "should be a simple assertion"() {
expect:
1 == 1
}
// fails
def "get dog names"() {
given:
GroovyShell shell = new GroovyShell()
def getDogNames = shell.parse(new File('src/main/scripts/parseDogs.gradle'))
expect:
getDogNames().size() == 2
}
}
When I try to build locally, I get the error:
ExampleTest > get dog names FAILED
org.spockframework.runtime.ConditionFailedWithExceptionError at ExampleTest.groovy:15
Caused by: groovy.lang.MissingMethodException at ExampleTest.groovy:15
The report generated under buildSrc/build/reports/tests/test
has a similar message:
Condition failed with Exception:
getDogNames().size() == 2
|
groovy.lang.MissingMethodException: No signature of method: parseDogs.call() is applicable for argument types: () values: []
Possible solutions: wait(), any(), wait(long), main([Ljava.lang.String;), tap(groovy.lang.Closure), each(groovy.lang.Closure)
at ExampleTest.get dog names(ExampleTest.groovy:15)
at ExampleTest.get dog names(ExampleTest.groovy:15)
Caused by: groovy.lang.MissingMethodException: No signature of method: parseDogs.call() is applicable for argument types: () values: []
Possible solutions: wait(), any(), wait(long), main([Ljava.lang.String;), tap(groovy.lang.Closure), each(groovy.lang.Closure)
... 1 more
Is there a way I can access the ext.parseDogs()
method in my test?
Upvotes: 0
Views: 140