我有以下脚本文件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()?
发布于 2017-07-20 08:52:56
我认为aScript.groovy和bScript.groovy应该在var中,而不是在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")
要在此时加载管道,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)
https://stackoverflow.com/questions/45142634
复制相似问题