我有以下脚本文件
aScript.groovy 和 bScript.groovy 都在vars文件夹中,这些脚本将从项目文件夹中的 jenkinsFile 调用
aScript.groovy
def call(){
return bScript()
}
bScript.groovy
def call(){
return "bar"
}
我正在使用gradle构建 build.gradle 文件,如下所示
apply plugin: 'groovy'
apply plugin: 'project-report'
repositories {
mavenCentral()
}
sourceSets {
main {
groovy {
srcDirs = ['src/main/groovy']
}
output.resourcesDir = "build/resources/test/lib/commons@master/vars"
}
test {
groovy {
srcDirs = ['src/test/groovy']
}
output.resourcesDir = "build/resources/test"
}
}
sourceSets.main.resources { srcDirs = ['vars', 'src/main/resources', 'resources'] }
sourceSets.test.resources { srcDirs = ['src/test/resources'] }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.9'
testCompile 'junit:junit:4.12'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
testCompile 'com.lesfurets:jenkins-pipeline-unit:1.0'
testCompile 'cglib:cglib-nodep:3.2.2'
testCompile 'org.objenesis:objenesis:1.2'
testCompile 'org.assertj:assertj-core:3.4.1'
compile ('org.jenkins-ci.main:jenkins-core:2.46.3'){
exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
}
compile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile 'org.slf4j:slf4j-log4j12:1.7.21'
compile 'commons-beanutils:commons-beanutils:1.9.3'
compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
}
测试类
class aScriptSpec extends Specification {
/**
* Delegate to the test helper
*/
@Delegate JenkinsPipelineTestHelper pipelineTestHelper
def setup() {
pipelineTestHelper = new JenkinsPipelineTestHelper()
pipelineTestHelper.setUp()
pipelineTestHelper.setJobVariables()
def library = library().name('commons')
.defaultVersion("master")
.allowOverride(true)
.implicit(false)
.targetPath(sharedLibs)
.retriever(localSource(sharedLibs))
.build()
helper.registerSharedLibrary(library)
helper.registerAllowedMethod('timestamps', [Closure.class], null)
helper.registerAllowedMethod('node', [Closure.class], null)
helper.registerAllowedMethod("stage", [String.class], null)
}
def "pipeline test"() {
given:
addEnvVar('workerLabel','testworker')
addEnvVar('BRANCH_NAME','PRbuild')
//to mock bScript
helper.registerAllowedMethod("bScript", [], null)//1. tried using registerAllowedMethod
bScript.metaClass.static.call = {return 'bar'} //2. tried using metaClass
binding.setVariable('bScript', new Object() { //3. tried using metaClass
def call() {
println "bScript call mocked"
}
})
when:
loadScript('Jenkinsfile')
then:
printCallStack()
}
}
class JenkinsPipelineTestHelper extends BasePipelineTest{
@Rule
public TemporaryFolder folder = new TemporaryFolder()
String sharedLibs = this.class.getResource('/lib').getFile()
void setUp() throws Exception {
scriptRoots += 'vars'
super.setUp()
}
/**
* Variables that Jenkins expects
*/
void setJobVariables() {...}
void addEnvVar(String name, String val) {...}
}
我的项目结构
app
\--build
\--classes
\--test
\--aScriptSpec.class
\--resources
\--test
\--lib
\--commons@master
\--vars
\-- aScript.groovy
|--bScript.groovy
|--src/test/groovy
\--aScriptSpec.groovy
|--vars
\--aScript.groovy
|--bScript.groovy
|--build.gradle
\--Jenkinsfile
我需要测试 aScript 功能,因此我想模拟 bscript(),我尝试使用
bScript.metaClass.static.call = {return 'bar'}
为其提供了groovy.lang.MissingPropertyException: No such property: bScript for class: aScriptSpec
我如何模拟bScript()?
最佳答案
我相信aScript.groovy和bScript.groovy应该在var [s]中而不是在var文件夹中。
.
└── vars
├── aScript.groovy
└── bScript.groovy
如果您想模拟bScript,则下面的代码就像一个符咒一样工作
helper.registerAllowedMethod("bScript", [], { return 'foo'})
UPD:
我只是复制了这一点。
我认为这不是您第一次模拟这样的方法:
helper.registerAllowedMethod("bScript", [], {'foo'})
或该特定对象出现在groovyClassPath中的任何内容
你打电话时
def script = loadScript("template/pipeline/template.groovy")
在此时加载管道,将使用lib中定义的对象再次更新classPath。所以我想你的模拟被覆盖了。
因此,这里的解决方法是将管道主体包装在诸如someMethod之类的方法中:
@Library("commons")
import java.io.File // or whatever
echo aScript() // this is not mocked
def someMethod() {
echo aScript() // this one could be mocked
}
return this
然后您的测试可能像:
@Test
void mock_bscript() throws Exception {
helper.registerAllowedMethod("bScript", [], {'foo'}) // this one doesn't work
def script = loadScript("template/pipeline/template.groovy")
helper.registerAllowedMethod("bScript", [], {'ololo'}) // this does
script.someMethod()
printCallStack()
}
所以调用堆栈将是
Loading shared library jenkins-commons with version master
template.run()
template.aScript()
aScript.bScript()
template.echo(bar)
template.someMethod()
template.aScript()
aScript.bScript()
template.echo(ololo)
关于gradle - 使用Spock模拟groovy脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45142634/