Skip to content

Unable to mock external shared library methods #105

@jevgarcia22

Description

@jevgarcia22

code under test: lint.groovy

def lintYamlFiles() {
    script {
        def detect = new detect()
        def changedFiles = detect.changedFiles()
        sh  '''
            #!/bin/bash -x
        '''
    }
}

I'm trying to mock execution of detect.changedFiles() like this:

class lintTest extends JenkinsPipelineSpecification   {
    def lint = null

    def setup() {
        lint = loadPipelineScriptForTest("../../src/org/xy/carbon/lint.groovy")
        explicitlyMockPipelineVariable('detect')             
    }

    def "test 01: successfully invoke "()   {
        setup:   getPipelineMock('detect.changedFiles').call('script') >> "test"
        when:    lint.lintYamlFiles()
        then:     1 * getPipelineMock('detect.changedFiles')(_)

have also tried: getPipelineMock('detect.changedFiles').call() >> "test"

executions fail with:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.914 s <<< FAILURE! - in lintTest
[ERROR] lintTest.test 01: successfully invoke   Time elapsed: 0.339 s  <<< ERROR!
groovy.lang.MissingMethodException:
No signature of method: org.xy.carbon.detect.script() is applicable for argument types: (org.xy.carbon.detect$_changedFiles_closure1) values: [org.xy.carbon.detect$_changedFiles_closure1@6917bb4]
Possible solutions: print(java.lang.Object), split(groovy.lang.Closure), print(java.lang.Object), print(java.io.PrintWriter), wait(), grep()
	at org.xy.carbon.detect.changedFiles(detect.groovy:9)
	at org.xy.carbon.lint.lintYamlFiles_closure1(lint.groovy:8)
	at org.xy.carbon.lint.lintYamlFiles_closure1(lint.groovy)
	at com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification.addPipelineMocksToObjects_closure1$_closure14(JenkinsPipelineSpecification.groovy:682)
	at org.xy.carbon.lint.lintYamlFiles(lint.groovy:5)
	at lintTest.test 01: successfully invoke (lintTest.groovy:38)

Is it possible to mock interactions with methods in external libraries?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions