Cannot get property 'values' on null object in JenkinsPipelineUnit

7.4k Views Asked by At

I have below module call xrayExportJiraFeature.groovy in my JenkinsSharedLib and call that module in my 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
}

This above module calling from javaPipeline.groovy

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

    jiraInstanceID = xrayExportJiraFeature(params.xrayJiraIssue)

Once I tried to run below unit test against above

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()
    }
}

At runtime I get below error at specified line above.

Error :

[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)

Note : I'm using JenkinsPipelineUnit to test a pipeline

1

There are 1 best solutions below

1
On BEST ANSWER

Adding below line to the test file (mocking) did the trick

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

Basically in the above step it properly mock jiraServerDetails variable