title: Spock testing exception handling in a Where block
body: <p>I am testing a service method which has a few dependencies; and I want to assert that if any of these dependencies throws an exception, the service method should return a default value.</p>
  
  <p>The service and the test I'd like to write look something like this.</p>
  
  <pre><code>static class Service {
      def dependency1
      def dependency2
      def dependency3
  
      def method() {
          try {
              def foo = dependency1.get()
              def bar = dependency2.get()
              def baz = dependency3.get()
              return " $foo $bar $baz "
          } catch (Exception e) {
              println e
              return ' default value '
          }
      }
  }
  
  def 'test Service error handling'() {
      given:
      def dependency1 = Mock(Supplier)
      def dependency2 = Mock(Supplier)
      def dependency3 = Mock(Supplier)
      def serviceUnderTest = new Service(dependency1: dependency1, dependency2: dependency2, dependency3: dependency3)
  
      when:
      def result = serviceUnderTest.method()
  
      then:
      result == ' default value '
      dependency1.get() &gt;&gt; closure1
      dependency2.get() &gt;&gt; closure2
      dependency3.get() &gt;&gt; closure3
  
      where:
      closure1                              | closure2                              | closure3
      {-&gt; throw new Exception('closure1') } | {-&gt; null }                            | {-&gt; null };
      {-&gt; null}                             | {-&gt; throw new Exception('closure2') } | {-&gt; null };
      {-&gt; null}                             | {-&gt; null}                             | {-&gt; throw new Exception('closure3') }
  }
  </code></pre>
  
  <p>This test doesn't work, because it causes the mocks to return literal closures rather than the results of those closures. Of course this is caused by the addition of the <code>where</code> block, as any mock can directly return the result of a single closure, i.e. <code>dependency1.get() &gt;&gt; { throw new Exception() }</code></p>
  
  <p>Am I forced to write this as three separate tests, or is there another way I can combine them?</p>