我在 JenkinsSharedLib 中有以下模块调用 xrayExportJiraFeature.groovy 并在我的 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 来测试管道