title: How to reset a mock invocation counter using Scalatra, Specs2, Mockito
body: <p>I never expected that I will need to ask a question on this site because everything is already answered normally but with Scalatra... I haven't find a lot of information so here it is:</p>
  
  <p>I'm not experienced with all that so maybe I'm missing something but from what I understand, if I want to test the API that I develop on Scalatra, I need to start the server everytime I run the test suit, right ?</p>
  
  <p>Second question, how can I reset the invocation counter on a method so I don't have to calculate how many times the method has been called since the beginning of the test suite ? Right now using this give me more than one because it counts the previous test.</p>
  
  <pre><code>there was one(*instance*).*method*(*parameter*)
  </code></pre>
  
  <p>I can still get around the problem by either counting or putting the test as first test for now but it's not a sustainable solution...</p>
  
  <p>Other thing that I found:
  Reset method on the mock... not found
  <a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#17" rel="nofollow">http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#17</a></p>
  
  <p>Isolating the test in a class scope:
  We need to add </p>
  
  <pre><code>val servlet = new Servlet(eventRepoMock)
  addServlet(servlet, "/*")
  </code></pre>
  
  <p>and we can't repeat the addServlet at every initialization 
  <a href="https://etorreborre.github.io/specs2/guide/SPECS2-3.5/org.specs2.guide.Isolation.html" rel="nofollow">https://etorreborre.github.io/specs2/guide/SPECS2-3.5/org.specs2.guide.Isolation.html</a></p>
  
  <p>Last thing that I try is:</p>
  
  <pre><code>servlet.repo = mock[EventRepo]
  </code></pre>
  
  <p>but <code>repo</code> being a value, I can't change it like this.</p>
  
  <p>Neither of these "solutions" feel very clean so I was wondering if someone had a genius idea that can solve that mess !?</p>
  
  <p>Thank you in advance !</p>
  
  <p>EDIT:
  Thanks to Eric's comment the above question are solve(that was easy) but now I have problem because I'm testing the get/post which are asynchronous call so resetting the mock does not happen at the right time... Any suggestion ?</p>
  
  <p>Here's a simplified version of the code:</p>
  
  <pre><code>class EventServiceSpec extends ScalatraSpec with Mockito with Before { def is = s2"""
  Event Service
  
  GET an existing event
  must return status 200                              $get_status200
  must return the event with id = :id                 $get_rightEventElement
  must call findById once on the event repository     $get_findByIdOnRepo
  """
  
  lazy val anEvent = Event(1, "Some Event"
  lazy val eventsBaseUrl = "/events"
  lazy val existingEventUrl = s"$eventsBaseUrl/${anEvent.id}"
  
  lazy val eventRepoMock = mock[EventRepository]
  
  lazy val servlet = new Servlet(eventRepoMock)
  addServlet(servlet, "/*")
  
  def before = {
      eventRepoMock.findById(anEvent.id) returns Option(anEvent)
      eventRepoMock.findById(unexistingId) returns None
      eventRepoMock.save(anEvent) returns Option(anEvent)
  }
  
  def get_status200 = get(existingEventUrl){
      status must_== 200
  }
  
  def get_findByIdOnRepo = get(existingEventUrl){
      // TODO count considering previous test... need to find a cleaner way
      there was three(eventRepoMock).findById(anEvent.id)
  }
  </code></pre>