title: Mock groovy script using spock
body: <p>I have the following script files
  <strong>aScript.groovy</strong> and <strong>bScript.groovy</strong> both are in vars folder, these scripts will be called from a <strong>jenkinsFile</strong>  in the project folder</p>
  
  <p><strong>aScript.groovy</strong></p>
  
  <pre><code>def call(){
    return bScript()
  }
  </code></pre>
  
  <p><strong>bScript.groovy</strong></p>
  
  <pre><code>def call(){
    return "bar"
  }
  </code></pre>
  
  <p>I am using gradle for build my <strong>build.gradle</strong> file look as follows</p>
  
  <pre><code>apply plugin: 'groovy'
  apply plugin: 'project-report'
  
  repositories {
      mavenCentral()
  }
  sourceSets {
      main {
          groovy {
              srcDirs = ['src/main/groovy']
          }
          output.resourcesDir = "build/resources/test/lib/commons@master/vars"
      }
      test {
          groovy {
              srcDirs = ['src/test/groovy']
          }
          output.resourcesDir = "build/resources/test"
      }
  }
  sourceSets.main.resources { srcDirs = ['vars', 'src/main/resources', 'resources'] }
  sourceSets.test.resources { srcDirs = ['src/test/resources'] }
  dependencies {
  
      compile 'org.codehaus.groovy:groovy-all:2.4.9'
  
      testCompile 'junit:junit:4.12'
      testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
      testCompile 'com.lesfurets:jenkins-pipeline-unit:1.0'
      testCompile 'cglib:cglib-nodep:3.2.2'
      testCompile 'org.objenesis:objenesis:1.2'
      testCompile 'org.assertj:assertj-core:3.4.1'
  
      compile ('org.jenkins-ci.main:jenkins-core:2.46.3'){
          exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
      }
  
      compile 'javax.servlet:javax.servlet-api:3.1.0'
      testCompile 'org.slf4j:slf4j-log4j12:1.7.21'
      compile 'commons-beanutils:commons-beanutils:1.9.3'
      compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
  
  }
  </code></pre>
  
  <p><strong>Test Classes</strong></p>
  
  <pre><code>class aScriptSpec extends Specification {
  
      /**
       * Delegate to the test helper
       */
      @Delegate JenkinsPipelineTestHelper pipelineTestHelper
  
      def setup() {
          pipelineTestHelper = new JenkinsPipelineTestHelper()
          pipelineTestHelper.setUp()
          pipelineTestHelper.setJobVariables()
          def library = library().name('commons')
                  .defaultVersion("master")
                  .allowOverride(true)
                  .implicit(false)
                  .targetPath(sharedLibs)
                  .retriever(localSource(sharedLibs))
                  .build()
          helper.registerSharedLibrary(library)
  
          helper.registerAllowedMethod('timestamps', [Closure.class], null)
          helper.registerAllowedMethod('node', [Closure.class], null)
          helper.registerAllowedMethod("stage", [String.class], null)
      }
  
      def "pipeline test"() {
  
          given:
              addEnvVar('workerLabel','testworker')
              addEnvVar('BRANCH_NAME','PRbuild')
  
              //to mock bScript
              helper.registerAllowedMethod("bScript", [], null)//1. tried using registerAllowedMethod
  
              bScript.metaClass.static.call = {return 'bar'} //2. tried using metaClass
  
              binding.setVariable('bScript', new Object() { //3. tried using metaClass
                  def call() {
                      println "bScript call mocked"
                  }
              })
  
  
          when:
  
              loadScript('Jenkinsfile')
          then:
              printCallStack()
      }
  }
  
  
  class JenkinsPipelineTestHelper extends BasePipelineTest{
  
      @Rule
      public TemporaryFolder folder = new TemporaryFolder()
  
      String sharedLibs = this.class.getResource('/lib').getFile()
      void setUp() throws Exception {
  
          scriptRoots += 'vars'
          super.setUp()
      }
      /**
       * Variables that Jenkins expects
       */
      void setJobVariables() {...}
  
  
  
      void addEnvVar(String name, String val) {...}
  
  
  }
  </code></pre>
  
  <p><strong>my project structure</strong></p>
  
  <pre><code>   app
        \--build
               \--classes
                        \--test
                               \--aScriptSpec.class
               \--resources
                        \--test
                               \--lib
                                     \--commons@master
  
   \--vars                                                                                   
     \-- aScript.groovy                                                                                 
     |--bScript.groovy
  |--src/test/groovy
                 \--aScriptSpec.groovy
  |--vars
         \--aScript.groovy
         |--bScript.groovy
  |--build.gradle
  \--Jenkinsfile
  </code></pre>
  
  <p>I need to test <strong>aScript</strong> functionality so i want to mock <strong>bscript()</strong> i tried using <code>bScript.metaClass.static.call = {return 'bar'}</code> its giving me <code>groovy.lang.MissingPropertyException: No such property: bScript for class: aScriptSpec</code></p>
  
  <p>how can i mock bScript()?</p>