无法在 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 变量

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...