问题描述
我有一个项目,我将逻辑拆分为多个 gradle 文件并将它们写入 ext{} 块。这是我正在做的事情的简化版本来说明我的问题:
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
}
}
以下是我与测试相关的内容:
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
}
}
当我尝试在本地构建时,出现错误:
ExampleTest > get dog names Failed
org.spockframework.runtime.ConditionFailedWithExceptionError at ExampleTest.groovy:15
Caused by: groovy.lang.MissingMethodException at ExampleTest.groovy:15
在 buildSrc/build/reports/tests/test
下生成的报告有类似的信息:
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(),each(groovy.lang.Closure)
... 1 more
有没有办法在我的测试中访问 ext.parseDogs()
方法?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)