Spock Mock funktioniert nicht für Komponententest

Ich erhalte seltsame Ergebnisse aus einem Spock-Unit-Test, den ichhabe gedach wurde durch einen Missbrauch von Groovy's @ verursacTupleConstructor Anmerkung. Dank der Hilfe eines anderen Benutzers sehe ich jedoch ein Problem mit der Art und Weise, wie Spock Mocks erstellt. Obwohl ich das Problem behoben habe, indem ich die injizierten Mocks durch echte Instanzen ersetzt habe, muss ich in der Tat dafür sorgen, dass die Mocks hier funktionieren.

Meine Hauptklassen:

@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}"
    }
}

Mein Spock-Test:

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 >> f1
        fizz2.name >> 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
    }
}

Aber wenn ich das starte bekomme ich folgenden Fehler:

Condition not satisfied:

"${f1} - ${f2}" == str
  |        |     |  |
  fizzy    buzzy |  null - null
                 false
                 <omitting details here for brevity>

Expected :null - null

Actual   :fizzy - buzzy

Irgendwelche Ideen, wo ich schief gehe?

Antworten auf die Frage(4)

Ihre Antwort auf die Frage