title: Throw IOException in multiple return results with Spock
body: <p>I'm testing error handling. I want the first call to <code>mockedObject.foo()</code> to throw a new IOException, and the second to return bar. I tried the following code,</p>
  
  <pre><code>mockedObject.foo() &gt;&gt;&gt; [{throw new IOException()}, bar]
  </code></pre>
  
  <p>But, when I run the test, I get an error stating that a closure cannot be cast to Bar,</p>
  
  <blockquote>
    <p>FooSpec$_$spock_feature_0_1_closure2 cannot be cast to Bar</p>
  </blockquote>
  
  <p>How can I mock this behavior with Spock?</p>
  
  <p>EDIT: After seeing the documentation referenced by tim_yates, I just changed the test to,</p>
  
  <pre><code>mockedObject.foo() &gt;&gt;&gt; firstBar &gt;&gt; {throw new IOException()} &gt;&gt; secondBar
  </code></pre>
  
  <p>This comes close enough to testing what I needed to test. The following code threw the same exception, so I'm guessing Spock is setting the return type of the mocked method based on the first object return.</p>
  
  <pre><code>mockedObject.foo() &gt;&gt;&gt; {throw new IOException()} &gt;&gt; secondBar
  </code></pre>