在 JenkinsPipelineUnit 中模拟 findFiles
Mocking findFiles in JenkinsPipelineUnit
目前我正在尝试注册 findFiles 步骤。
我的设置如下:
src/
test/
groovy/
TestJavaLib.groovy
vars/
javaLib.groovy
javaApp.jenkinsfile
里面TestJavaApp.groovy我有:
...
import com.lesfurets.jenkins.unit.RegressionTest
import com.lesfurets.jenkins.unit.BasePipelineTest
class TestJavaLibraryPipeline extends BasePipelineTest implements RegressionTest {
// Some overridden setUp() which loads shared libs
// and registers methods referenced in javaLib.groovy
void registerPipelineMethods() {
...
def fileList = [new File("testFile1"), new File("testFile2")]
helper.registerAllowedMethod('findFiles', { f -> return fileList })
...
}
}
我的 javaLib.groovy 包含当前失败的部分:
...
def pomFiles = findFiles glob: "target/publish/**/${JOB_BASE_NAME}*.pom"
if (pomFiles.length < 1) { // Fails with java.lang.NullPointerException: Cannot get property 'length' on null object
error("no pom file found")
}
...
我尝试了多次闭包返回各种对象,但每次我都遇到 NPE。
问题是 - 如何正确注册 "findFiles" 方法?
N.B。我对 groovy 中的模拟和闭包非常陌生。
查看 source code and examples on GitHub, I see a few overloads of the method (here):
void registerAllowedMethod(String name, List<Class> args = [], Closure closure)
void registerAllowedMethod(MethodSignature methodSignature, Closure closure)
void registerAllowedMethod(MethodSignature methodSignature, Function callback)
void registerAllowedMethod(MethodSignature methodSignature, Consumer callback)
您似乎没有在通话中注册正确的签名。我真的很惊讶你没有得到 MissingMethodException
你当前的呼叫模式。
您需要在注册时添加剩余的方法签名。 findFiles
方法采用 Map
个参数(glob: "target/publish/**/${JOB_BASE_NAME}*.pom"
是 Groovy 中的映射文字)。注册该类型的一种方法是这样的:
helper.registerAllowedMethod('findFiles', [Map.class], { f -> return fileList })
我也遇到了同样的问题。但是,我能够使用以下方法签名模拟 findFiles() 方法:
helper.registerAllowedMethod(method('findFiles', Map.class), {map ->
return [['path':'testPath/test.zip']]
})
所以我找到了一种在需要长度时如何模拟 findFiles
的方法 属性:
helper.registerAllowedMethod('findFiles', [Map.class], { [length: findFilesLength ?: 1] })
这也允许在测试中更改 findFilesLength
变量以测试管道中的不同条件,就像我的 OP 中的那样。
目前我正在尝试注册 findFiles 步骤。 我的设置如下:
src/
test/
groovy/
TestJavaLib.groovy
vars/
javaLib.groovy
javaApp.jenkinsfile
里面TestJavaApp.groovy我有:
...
import com.lesfurets.jenkins.unit.RegressionTest
import com.lesfurets.jenkins.unit.BasePipelineTest
class TestJavaLibraryPipeline extends BasePipelineTest implements RegressionTest {
// Some overridden setUp() which loads shared libs
// and registers methods referenced in javaLib.groovy
void registerPipelineMethods() {
...
def fileList = [new File("testFile1"), new File("testFile2")]
helper.registerAllowedMethod('findFiles', { f -> return fileList })
...
}
}
我的 javaLib.groovy 包含当前失败的部分:
...
def pomFiles = findFiles glob: "target/publish/**/${JOB_BASE_NAME}*.pom"
if (pomFiles.length < 1) { // Fails with java.lang.NullPointerException: Cannot get property 'length' on null object
error("no pom file found")
}
...
我尝试了多次闭包返回各种对象,但每次我都遇到 NPE。 问题是 - 如何正确注册 "findFiles" 方法?
N.B。我对 groovy 中的模拟和闭包非常陌生。
查看 source code and examples on GitHub, I see a few overloads of the method (here):
void registerAllowedMethod(String name, List<Class> args = [], Closure closure)
void registerAllowedMethod(MethodSignature methodSignature, Closure closure)
void registerAllowedMethod(MethodSignature methodSignature, Function callback)
void registerAllowedMethod(MethodSignature methodSignature, Consumer callback)
您似乎没有在通话中注册正确的签名。我真的很惊讶你没有得到 MissingMethodException
你当前的呼叫模式。
您需要在注册时添加剩余的方法签名。 findFiles
方法采用 Map
个参数(glob: "target/publish/**/${JOB_BASE_NAME}*.pom"
是 Groovy 中的映射文字)。注册该类型的一种方法是这样的:
helper.registerAllowedMethod('findFiles', [Map.class], { f -> return fileList })
我也遇到了同样的问题。但是,我能够使用以下方法签名模拟 findFiles() 方法:
helper.registerAllowedMethod(method('findFiles', Map.class), {map ->
return [['path':'testPath/test.zip']]
})
所以我找到了一种在需要长度时如何模拟 findFiles
的方法 属性:
helper.registerAllowedMethod('findFiles', [Map.class], { [length: findFilesLength ?: 1] })
这也允许在测试中更改 findFilesLength
变量以测试管道中的不同条件,就像我的 OP 中的那样。