title: Spock: creating mocked objects at run-time
body: <p>I have a class <code>Foo</code> that has a method <code>Bar makeBar(String id)</code>. As you can guess, <code>makeBar</code> creates a new <code>Bar</code> object with the id <code>id</code>. <code>Bar</code> has a getter for <code>id</code>.</p>
  
  <p>For the purpose of my test I need to mock <code>Foo</code>. I would like the mocked <code>makeBar</code> method to create mocked <code>Bar</code> objects for which the getter returns the correct <code>id</code> (the one that was given to <code>makeBar</code>).</p>
  
  <hr>
  
  <p>So to be clear, I want to create a mocked instance <code>foo</code> of <code>Foo</code> such that</p>
  
  <pre><code>foo.makeBar(someId)
  </code></pre>
  
  <p>returns a mocked <code>Bar</code> object <code>bar</code> for which</p>
  
  <pre><code>bar.getId() == someID
  </code></pre>
  
  <p>Is there a way to do this with Spock, or should I stub everything?</p>