简体   繁体   中英

Testing that a plugin function is called in jenkins shared library

I'm trying to write a unit test for a util function in a Jenkins shared library. The util function, calls the Office365Connector plugin in Jenkins.

My util function looks like this:

#!/usr/bin/env groovy

import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates

def call(BuildNotificationSettings options, BuildStates status) {

    options.teamsChannelWebhookUrlList.each {
        office365ConnectorSend (
            status: status.toString(),
            webhookUrl: "$it",
            color: BuildStates.valueOf(status.toString()).color,
            message: "Build ${status.toString()}: ${JOB_NAME} - ${BUILD_DISPLAY_NAME}<br>Pipeline duration: ${currentBuild.durationString}"
        )
    }

}

The use case that I'm trying to test is that the office365ConnectorSend function is called.

I've tried the following approach:

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates

public class _sendTeamsMessageSpec extends JenkinsPipelineSpecification {

    def _sendTeamsMessage = null

    def setup() {
        _sendTeamsMessage = loadPipelineScriptForTest("vars/_sendTeamsMessage.groovy")
        def office365ConnectorSend = Mock(office365ConnectorSend)
    }

    def "returns without sending no build notification settings are passed" () {
        given:
            def options = new BuildNotificationSettings(
                shouldSendNotification: null,
                teamsChannelWebhookUrlList: null
            )
        when:
            def result = _sendTeamsMessage(options, null)
        then:
            0 * explicitlyMockPipelineVariable("office365ConnectorSend")(_)
    }
}

running this on Jenkins gives me a java.lang.IllegalStateException: There is no pipeline variable mock for [office365ConnectorSend]. , what am I doing wrong in this approach?

After following this thread, I ended up calling the explicitlyMockPipelineStep function on the office365ConnectorSend plugin function. This made the function visible in the tests and I was able to resume testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM