title: Grails Unit Testing Pass By Reference Doesn't Work When Mocking
body: <p>I have a unit test where I'm mocking a service class to save a Domain. Originally, my controller method looked like this:</p>
  
  <pre><code>def save(Long organizationId, Convention convention) {
    conventionService.save(organizationId, convention)
  
    if (convention.hasErrors()) {
      response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
      respond convention.errors
    } else {
      response.status = HttpStatus.CREATED.value()
      respond convention
    }
  }
  </code></pre>
  
  <p>Generally, this works because Java is pass by reference so the <code>convention</code> passed into the save method is the same <code>convention</code> object throughout the method. However, when mocking the <code>conventionService.save</code> method, pass by reference doesn't work. Adjusting my method to account for this:</p>
  
  <pre><code>def save(Long organizationId, Convention convention) {
    convention = conventionService.save(organizationId, convention)
  
    if (convention.hasErrors()) {
      response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
      respond convention.errors
    } else {
      response.status = HttpStatus.CREATED.value()
      respond convention
    }
  }
  </code></pre>
  
  <p>allows my test to pass because the <code>convention</code> object is what I'm expecting from my mock:</p>
  
  <pre><code>1 * service.save(1, _) &gt;&gt; new Convention(
         id: 1,
         name: 'Con 1',
         description: 'This is a pretty cool convention, everyone should go',
         startDate: new Date(),
         endDate: new Date()+10,
         organization: organization)
  </code></pre>
  
  <p>My question is, is this expected behavior or a bug that I should be reporting?</p>