无法在 JenkinsPipelineUnit 中的空对象上获取 属性 'values'

Cannot get property 'values' on null object in JenkinsPipelineUnit

我有以下模块调用 xrayExportJiraFeature.groovy 在我的 JenkinsSharedLib 中并在我的 javaPipeline

中调用该模块
def call(xrayJiraIssue) {

    def jiraInstanceID = ""

    echo 'curl -k -L -X GET \'https://jenkinsci-mypipeline.service.test/descriptorByName/com.xpandit.plugins.xrayjenkins.task.XrayImportBuilder/fillServerInstanceItems\''
    def jiraServerDetailsOutput = sh returnStdout: true, script: 'curl -k -L -X GET \'https://jenkinsci-mypipeline.service.test/descriptorByName/com.xpandit.plugins.xrayjenkins.task.XrayImportBuilder/fillServerInstanceItems\''

    def jiraServerDetails = readJSON text: "${jiraServerDetailsOutput}"
    jiraInstanceID = jiraServerDetails.values[0].value
    echo "Jira Instance ID...  ${jiraInstanceID}"
    step([$class: 'XrayExportBuilder', filePath: 'src/test/resources/xray_features', issues: "${xrayJiraIssue}", serverInstance: "${jiraInstanceID}"])

    return jiraInstanceID
}

上述模块调用自 javaPipeline.groovy

string(defaultValue: 'ABC-0000', description: 'Xray Test Issue to execute', name: 'xrayJiraIssue')

    jiraInstanceID = xrayExportJiraFeature(params.xrayJiraIssue)

有一次我尝试运行下面的单元测试针对上面的

import com.lesfurets.jenkins.unit.*
import org.junit.Before
import org.junit.*

class XrayExportJiraFeatureTest extends BasePipelineTest {

    Script script

    @Override
    @Before
    void setUp() throws Exception {

        setScriptRoots(['src', 'vars', 'test/groovy', 'test/groovy/resources'] as String[])
        setScriptExtension('groovy')
        super.setUp()
    }

    public void loadPipeline() {
        script = loadScript("xrayExportJiraFeature.groovy")
        helper.registerAllowedMethod('step', [LinkedHashMap]) {}
        helper.registerAllowedMethod('readJSON', [LinkedHashMap]) {}
        helper.registerAllowedMethod('values', [LinkedHashMap]) {}
        binding.setVariable('xrayJiraIssue', '123')
    }

    @Test
    public void xrayExportJiraFeatureTest_InputParamsNull() {
        loadPipeline()
        script.call(null)            **************  error line
        printCallStack()
        assertJobStatusSuccess()
    }

    @Test
    public void xrayExportJiraFeatureTest_InputParamsNotNull() {
        loadPipeline()
        script.call("ABC-123")     **************  error line
        printCallStack()
        assertJobStatusSuccess()
    }
}

在 运行 时,我在上面的指定行出现以下错误。

错误:

[ThreadedStreamConsumer] ERROR org.apache.maven.plugin.surefire.SurefirePlugin - xrayExportJiraFeatureTest_InputParamsNull(XrayExportJiraFeatureTest)  Time elapsed: 0.03 s  <<< ERROR!
java.lang.NullPointerException: Cannot get property 'values' on null object
    at XrayExportJiraFeatureTest.xrayExportJiraFeatureTest_InputParamsNull(XrayExportJiraFeatureTest.groovy:27)

[ThreadedStreamConsumer] ERROR org.apache.maven.plugin.surefire.SurefirePlugin - xrayExportJiraFeatureTest_InputParamsNotNull(XrayExportJiraFeatureTest)  Time elapsed: 0.02 s  <<< ERROR!
java.lang.NullPointerException: Cannot get property 'values' on null object
    at XrayExportJiraFeatureTest.xrayExportJiraFeatureTest_InputParamsNotNull(XrayExportJiraFeatureTest.groovy:35)

注意:我正在使用 JenkinsPipelineUnit 来测试管道

将以下行添加到测试文件(模拟)就可以了

helper.registerAllowedMethod("readJSON", [LinkedHashMap]) {["something": "something", "values":[["value":"something"]]]}

基本上在上面的步骤中它正确地模拟了 jiraServerDetails 变量