Spock Mock not working for unit test
<p>I am getting weird results from a Spock unit test that I <em>thought</em> was being caused by a misuse of Groovy's <a href="http://docs.groovy-lang.org/latest/html/gapi/groovy/transform/TupleConstructor.html" rel="nofollow"><code>TupleConstructor</code></a> annotation. However, thanks to the help of another user, I see it is a problem with the way Spock is creating mocks. Although I have fixed the issue by replacing the injected mocks with real instances, I need to in fact get mocks working here.</p>

<p>My main classes:</p>

<pre><code>@Canonical
@TupleConstructor(callSuper = true)
abstract class Vehicle {
    Long id
}

@Canonical
@TupleConstructor(callSuper = true, includeSuperProperties = true)
abstract class Foobaz extends Vehicle {
    String name
    String label
    String description
}

@Canonical
@TupleConstructor(callSuper = true, includeSuperProperties = true)
class Fizz extends Foobaz {
    // This is an empty class that creates a meaningful name over the
    // abstract Foobaz parent class. This may seem like bad design in
    // this analogy, but I assure you it makes sense (from a Domain-Driven
    // Design perspective) in my actual application.
}

@Canonical
@TupleConstructor(callSuper = true, includeSuperProperties = true)
class Car extends Vehicle {
    Fizz fizz1
    Fizz fizz2

    @Override
    String toString() {
        "${fizz1.name} - ${fizz2.name}"
    }
}
</code></pre>

<p>My Spock test:</p>

<pre><code>class CarSpec extends Specification {
    def "toString() generates a correct string"() {
        given: "a Car with some mocked dependencies"
        String f1 = 'fizzy'
        String f2 = 'buzzy'
        Fizz fizz1 = Mock(Fizz)
        Fizz fizz2 = Mock(Fizz)

        fizz1.name &gt;&gt; f1
        fizz2.name &gt;&gt; f2

        Car car = new Car(1L, fizz1, fizz2)

        when: "we call toString()"
        String str = car.toString()

        then: "we get a correctly formatted string"
        "${f1} - ${f2}" == str
    }
}
</code></pre>

<p>But when I run this I get the following failure/error:</p>

<pre><code>Condition not satisfied:

"${f1} - ${f2}" == str
  |        |     |  |
  fizzy    buzzy |  null - null
                 false
                 &lt;omitting details here for brevity&gt;

Expected :null - null

Actual   :fizzy - buzzy
</code></pre>

<p>Any ideas where I'm going awry?</p>