Spock Web Console

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

MyPublisherSpec

Published 3 months ago by lajos with tags spock
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

class MyPublisherSpec extends Specification {
  
    Publisher publisher = new Publisher()
    Subscriber subscriber1 = Mock()
    Subscriber subscriber2 = Mock()

    def setup() {
        publisher.subscribers.add(subscriber1)
        publisher.subscribers.add(subscriber2)
    }

    def "let's see the interactions"() {
        when:
        publisher.send("hello")

        then:
        1 * subscriber1.receive("hello")
        1 * subscriber2.receive("hello")
    }

    class Publisher {
        List<Subscriber> subscribers = []
        void send(String message) {
            subscribers*.receive(message)
        }
    }
    
    interface Subscriber {
        void receive(String message)
    }
}