title: Spock not mocking concrete classes correctly?
body: <p>I work on a Java project, and have started writing my unit tests in Groovy, using the Spock framework. But I'm having an issue with Spock's mocking functionality, and hoping someone can figure out what I'm doing wrong.</p>
  
  <p>I have three java classes: a <code>FooContext</code> (that contains a <code>foo</code> property), a <code>HasFooContext</code> class (that contains a <code>fooContext</code> property), and a <code>FooService</code> that inherits from <code>HasFooContext</code> (and has an operation that calls the <code>fooContext</code>):</p>
  
  <pre><code>public class FooContext {
    private Object foo = new Object();
    public Object getFoo() {
      return foo;
    }
  }
  
  public abstract class HasFooContext {
    private FooContext fooContext;
    public void setFooContext(FooContext fooContext) {
      this.fooContext = fooContext;
    }
    public Object getFoo() {
      Object foo = fooContext.getFoo();
      assert foo != null : "no foo available";
      return foo;
    }
  }
  
  public class FooService extends HasFooContext {
    public void doFoo() {
      getFoo();
    }
  }
  </code></pre>
  
  <p>It can be seen here that the <code>doFoo</code> method in <code>FooService</code> calls the <code>getFoo</code> method in the base class <code>HasFooContext</code>, which in turn calls the <code>getFoo</code> method of its <code>fooContext</code> property. It also asserts that the value returned from <code>fooContext.getFoo()</code> is not null.</p>
  
  <p>I wrote the following unit test in Java, using Mockito, to verify that calling <code>doFoo</code> will invoke the <code>fooContext.getFoo()</code> method:</p>
  
  <pre><code>public class FooServiceJavaUnitTest {
  
    private FooContext fooContext;
    private FooService fooService;
  
    @Before
    public void setup() {
      Object foo = new Object();
  
      fooContext = mock(FooContext.class);
      when(fooContext.getFoo()).thenReturn(foo);
  
      fooService = new FooService();
      fooService.setFooContext(fooContext);
    }
  
    @Test
    public void doFooInvokesGetFoo() {
      fooService.doFoo();
      verify(fooContext, times(1)).getFoo();
    }
  
  }
  </code></pre>
  
  <p>This test runs successfully, as expected.</p>
  
  <p>I then wrote the following unit test in Groovy, using Spock:</p>
  
  <pre><code>class FooServiceGroovyUnitTest extends Specification {
  
    private FooContext fooContext;
    private FooService fooService;
  
    def setup() {
      // Create a mock FooContext.
      fooContext = Mock(FooContext)
      fooContext.getFoo() &gt;&gt; new Object()
  
      fooService = new FooService()
      fooService.fooContext = fooContext
    }
  
    def "doFoo invokes getFoo"() {
      when: "call doFoo"
      fooService.doFoo()
  
      then: "getFoo is invoked"
      1 * fooContext.getFoo()
    }
  
  }
  </code></pre>
  
  <p>This test fails as follows:</p>
  
  <p><em>FooServiceGroovyUnitTest.doFoo invokes getFoo:21 no foo available</em></p>
  
  <p>That is, the following fails when using Groovy/Spock, but not when using Java/Mockito:</p>
  
  <pre><code>public abstract class HasFooContext {
    ...
    public Object getFoo() {
      Object foo = fooContext.getFoo();
      assert foo != null : "no foo available";
      return foo;
    }
  }
  </code></pre>
  
  <p>The <code>foo</code> property in <code>FooContext</code> is not final, so the <code>getFoo()</code> method should not be final, so the generated proxies should have no problem intercepting that method (as per the Java/Mockito test).</p>
  
  <p>Note that if I replace the mock <code>FooContext</code> with spying on a concrete <code>FooContext</code> as follows:</p>
  
  <pre><code>class FooServiceGroovyUnitTest extends Specification {
    ...
    def setup() {
      // Spy on a real FooContext.
      fooContext = Spy(FooContext)
  
      fooService = new FooService()
      fooService.fooContext = fooContext
    }
  }
  </code></pre>
  
  <p>then the Groovy/Spock unit test passes. Which suggests that the behaviour is different when mocking a concrete class vs spying on a concrete class. The Spock documentation mentions that the Mock api supports both interfaces (using dynamic proxies) and classes (using CGLIB).</p>
  
  <p>As far as I can see, the Java/Mockito unit test and the Groovy/Spock unit tests are equivalent, but I cannot get the Groovy/Spock unit test to pass when mocking the <code>FooContext</code> class.</p>
  
  <p>Any suggestions on what I am doing wrong?</p>