Spock Web Console

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

StubExample

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

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