【问题标题】:Mocking findFiles in JenkinsPipelineUnit在 JenkinsPipelineUnit 中模拟 findFiles
【发布时间】:2018-02-13 17:05:42
【问题描述】:

目前我正在尝试注册 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”方法?

注意我对 groovy 中的模拟和闭包非常陌生。

【问题讨论】:

    标签: jenkins groovy mocking jenkins-pipeline jenkins-pipeline-unit


    【解决方案1】:

    查看source code and examples on GitHub,我看到了一些方法的重载(here):

    1. void registerAllowedMethod(String name, List&lt;Class&gt; args = [], Closure closure)
    2. void registerAllowedMethod(MethodSignature methodSignature, Closure closure)
    3. void registerAllowedMethod(MethodSignature methodSignature, Function callback)
    4. 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 })
    

    【讨论】:

    • 即使我尝试不同的签名,我仍然会得到 NPE。我的想法是从闭包返回的值不会传递给 findFiles ,而 findFiles 又不会传递给 pomFiles 变量,从而导致 NPE。但由于缺乏经验,我还不知道如何解决它。
    • 我还在 PipelineUnit 存储库上打开了帮助请求:github.com/jenkinsci/JenkinsPipelineUnit/issues/91。但仍然提到那里的建议没有帮助。我想有关如何调试此问题的一些指示也会有所帮助。
    • @zrks 你找到解决方案了吗?我也面临类似的问题。我在扩展 BasePipelineTest 的类中添加了 void registerDeclarativeMethods() { helper.registerAllowedMethod('inside', [String.class], null) },但我仍然收到 groovy.lang.MissingMethodException: No signature of method: java.lang .String.inside() 适用于参数类型:(String, com.dsl.shared.WindowService$_createWindow_closure15$_closure25) 值:[-u root, com.dsl.shared.WindowService$_createWindow_closure15$_closure25@60bd273d]跨度>
    • @Angelina 是的,这是我的问题的解决方案 - github.com/jenkinsci/JenkinsPipelineUnit/issues/91 btw,检查什么参数类型采用 'inside' 方法
    【解决方案2】:

    我也遇到了同样的问题。但是,我能够使用以下方法签名来模拟 findFiles() 方法:

    helper.registerAllowedMethod(method('findFiles', Map.class), {map ->
                return [['path':'testPath/test.zip']]
            })
    

    【讨论】:

      【解决方案3】:

      所以我找到了一种在需要长度属性时如何模拟 findFiles 的方法:

      helper.registerAllowedMethod('findFiles', [Map.class], { [length: findFilesLength ?: 1] })

      这还允许在测试中更改 findFilesLength 变量以测试管道中的不同条件,例如我的 OP 中的条件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 2010-12-09
        相关资源
        最近更新 更多