Spock Web Console

subscribe to the feed Subscribe
CustomerOrderPlacementSpec (via #spockwebconsole)
tweet this script Tweet

CustomerOrderPlacementSpec

Published 1 year ago by mario
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

interface OrderPlacementService {
    boolean placeOrder(Order order)
}

class Customer {

    String firstName
    String lastName
    List<Order> orders = []

    OrderPlacementService orderPlacementService

    void placeOrder(Order order) {
        orders << order
        orderPlacementService.placeOrder(order)
    }

}

class Order {
    int amount
}

// ...

 
class MyFirstSpec extends Specification {
  def 'When an order is placed, the the order placement sytem will get notified'() {

    given: 'a mocked order placement system'
    def orderPlacementServiceMock = Mock(OrderPlacementService)

    and: 'a customer'
    def mario = new Customer(
        firstName: "Mario", 
        lastName: "David", 
        orderPlacementService: orderPlacementServiceMock
    )

    and: 'an order'
    def order = new Order(amount: 100)

    when: 'the order is placed'
    mario.placeOrder(order)

    then: 'the order placement system will get notified'
    1 * orderPlacementServiceMock.placeOrder(order)
  }  
}