Spock Web Console

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

StubExamplev2

Published 2 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 FooControllerSpec extends Specification {
  
  def "stub example"() {
    given: "the collaborating service is a stub"
    def stubFooService = Stub(FooService) {
      doSomething(_) >> "here you go!"
    }
    
    and: "having a real controller using the stub service"
    def controller = new FooController()
    controller.fooService = stubFooService

    expect: "the controller forwards the result of the stub"
    controller.doSomething() == "here you go!"
  }

  interface FooService {
    String doSomething(String name)
  }

  class FooController {
    FooService fooService
    def doSomething() {
      return fooService.doSomething("with me")
    }
  }
}