title: Spock testing: Too many invocation
body: <p>I wrote service for manual  requeuing events from one queue to another.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ReQueueService {
    private final RabbitTemplate rabbitTemplate;
  
    public void retry() {
      InfoLog infoLog;
      while (rabbitTemplate != null &amp;&amp; 
        (infoLog = (InfoLog) rabbitTemplate.receiveAndConvert(EVENT_WAITING_FOR_REQUEUE)) != null
      ) {
        rabbitTemplate.convertAndSend(SOME_QUEUE, infoLog.getSomeEvent());
      }
    }
  }
  </code></pre>
  
  <p>The problem I am facing is getting:</p>
  
  <pre class="lang-none prettyprint-override"><code>Too many invocations for:
  
  1 * rabbitTemplate.convertAndSend(SOME_QUEUE, _ as SomeEvent) &gt;&gt; {
        arguments -&gt;
          assert infoLog.getSomeEvent() == arguments[1]
      }   (2 invocations)
  
  Matching invocations (ordered by last occurrence):
  
  2 * rabbitTemplate.convertAndSend(SOME_QUEUE, ...
  </code></pre>
  
  <p>while my code in test looks like this:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>class ReQueueServiceTest extends Specification {
    def "should resend single event to some queue" () {
      given:
      InfoLog infoLog = Fixtures.createInfoLog()
      def rabbitTemplate = Mock(RabbitTemplate){
        receiveAndConvert(EVENT_WAITING_FOR_REQUEUE) &gt;&gt; { infoLog }
      }
      ReQueueService reSyncService = new ReQueueService(rabbitTemplate)
  
      when:
      reSyncService.retry()
  
      then:
      1 * rabbitTemplate.convertAndSend(SOME_QUEUE, _ as SomeEvent) &gt;&gt; {
        arguments -&gt;
          assert infoLog.getSomeEvent() == arguments[1]
      }
    }
  }
  </code></pre>
  
  <p>The question is why I have 2 invocations, if I stubb only one event?</p>
  
  <p>EDIT:</p>
  
  <p>link to repo with example: <a href="https://gitlab.com/bartekwichowski/spock-too-many" rel="nofollow noreferrer">https://gitlab.com/bartekwichowski/spock-too-many</a></p>