title: Groovy shared library testing pipeline step method with Spock
body: <p>I have shared library that calls pipeline step method(withCredentials).I am trying to test withCredentails method is being called correctly with sh scripts on calling myMethodToTest but facing error:</p>
  
  <pre><code> class myClass implements Serializable{
      def steps
      public myClass(steps) {this.steps = steps}
  
      public void myMethodToTest(script, String credentialsId) {
          steps.withCredentials([[$class: ‘UsernamePasswordMultiBinding’, credentialsId: "${credentialsId}", usernameVariable: ‘USR’, passwordVariable: ‘PWD’]]) {
               steps.sh """
                  export USR=${script.USR}
                  export PWD=${script.PWD}
                  $mvn -X clean deploy
               """
            }
       }
  }
  </code></pre>
  
  <p>//Mocking </p>
  
  <pre><code>class Steps {
     def withCredentials(List args, Closure closure) {}
  }
  
  class Script {
      public Map env = [:]
  }
  </code></pre>
  
  <p>//Test case</p>
  
  <pre><code>def "testMyMethod"(){
          given:
          def steps = Mock(Steps)
          def script = Mock(Script)
          def myClassObj = new myClass(steps)
          script.env['USR'] = "test-user"
  
          when:
          def result = myClassObj.myMethodToTest(script, credId)
  
          then:
          1 * steps.withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "mycredId", usernameVariable: 'USR', passwordVariable: 'PWD']])  
          1 * steps.sh(shString)
  
          where:
          credId | shString
          "mycredId" | "export USR='test-user'"
  </code></pre>
  
  <p>//Error</p>
  
  <pre><code>Too few invocations for:
  
  1 * steps.withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "mycredId", usernameVariable: ‘USR’, passwordVariable: ‘PWD’]])   (0 invocations)
  
  Unmatched invocations (ordered by similarity):
  
  1 * steps.withCredentials([['$class':'UsernamePasswordMultiBinding', 'credentialsId':mycredId, 'usernameVariable’:’USR’, 'passwordVariable':'PWD’]]
  </code></pre>