Skip to content

It is impossible to mock exisiting local function in declarative pipeline #461

Open
@grzegorzgrzegorz

Description

Hi,
Given is the pipeline which contains function inside the simpleDeclarativePipeline.pipeline file:

pipeline {
    agent { node { label 'test1' } }
    stages {
      stage('sdfsdf') {
        steps {
          githubNotify status: 'PENDING', context: 'Pipeline'
            runFunc()
        }
      }
    }
  }

  def runFunc(){
      echo "inside runFunc"
      doSomeStuff()
  }

When runFunc is mocked:

void setUp() throws Exception {

        baseScriptRoot = 'unit-testing-jenkinsfile'
        scriptRoots += 'src/main/groovy'
        scriptExtension = 'pipeline'

        super.setUp()

        helper.registerAllowedMethod("runFunc")
        helper.registerAllowedMethod("githubNotify", [Map.class], null)

    }

    @Test
    void should_execute_without_errors() throws Exception {
        def script = runScript("simpleDeclarativePipeline.pipeline")
        printCallStack()
        assertJobStatusSuccess()
    }

Then mocked function is not executed but real one instead and thus exception is thrown:
groovy.lang.MissingMethodException: No signature of method: simpleDeclarativePipeline.doSomeStuff() is applicable for argument types: () values: []

When runFunc inside pipeline is deleted:

 pipeline {
    agent { node { label 'test1' } }
    stages {
      stage('sdfsdf') {
        steps {
          githubNotify status: 'PENDING', context: 'Pipeline'
            runFunc()
        }
      }
    }
  }

Then runFunc is mocked as expected.

I tried newest version of JenkinsPipelineUnit: 1.13

This is a problem for me as I have very rich local functions I want to mock during pipeline testing. I do not want to mock functions like doSomeStuff in the example.
If this is a bug - do you know any workaround for this issue ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions