Nunuzac
Nunuzac

Reputation: 13

Jenkins spock: Mocking http-request-plugin's httpRequest

Somewhere in my shared library I got a helper class like this:

class Helper {

    def script

    Helper(script) {
        this.script = script
    }

    void sendTemplate(String webhook, String template, Map<String, String> values, TemplateMapper mapper) {
        def body = mapper.map(template, values)
        def resp = script.httpRequest(contentType: 'APPLICATION_JSON', httpMode: 'POST',
            requestBody: body, url: webhook)
        if (resp.status != 200) {
            throw new UnableToNotifyException()
        }
    }

}

I'm trying to test said class like so:

class HelperSpec extends JenkinsPipelineSpecification {

    def helper

    def setup() {
        helper = new Helper(this)
    }

    def "a test"() {
        setup:
            def webhook = 'aWebhook'
            def template = '%replaceMe'
            def values = ['%replaceMe': 'hello world!']
            def mapper = new SimpleTemplateMapper()
            getPipelineMock('httpRequest')(_) >> [status: 200]

        when:
            helper.sendTemplate(webhook, template, values, mapper)

        then:
            1 * getPipelineMock('httpRequest')(_)
    }

}

I'm using gradle and my build.gradle file has

testImplementation 'org.jenkins-ci.plugins:http_request:1.10@jar'

Other steps' tests run perfectly but with this one I always get

java.lang.IllegalStateException: There is no pipeline step mock for [httpRequest].
    1. Is the name correct?
    2. Does the pipeline step have a descriptor with that name?
    3. Does that step come from a plugin? If so, is that plugin listed as a dependency in your pom.xml?
    4. If not, you may need to call explicitlyMockPipelineStep('httpRequest') in your test's setup: block.

And when I use explicitlyMockPipelineStep('httpRequest') I get a null pointer exception, because, I presume, the default mock returns a null.

Is there anything I'm missing in the test to get it working? Thanks in advance!!!

Upvotes: 0

Views: 373

Answers (0)

Related Questions