Rob Fletcher git clone  http://github.com/robfletcher/ggug-spock-examples
Expressive testing  language Easy to learn Usable from unit to end-to-end level Leverages Groovy language features Runs with JUnit: compatible with IDEs, build tools & CI Extensible for specialized testing scenarios "Everything is questioned, and only the essential is kept"
Classes extend  spock.lang.Specification Feature methods: def "name in quotes"() separated into labelled blocks Lifecycle:  setup cleanup setupSpec cleanupSpec
given :   preconditions, data fixtures, etc. when :   actions that trigger some outcome then :   makes assertions about outcome expect :   short alternative to when & then where :   applies varied inputs and :   sub-divides other blocks setup :   alias for given cleanup : post-conditions, housekeeping, etc.
import spock.lang.* class TotalizerSpec  extends Specification  {    def totalizer = new Totalizer()    def  "adding an item increments the total" () {      when:   "a product is added"      totalizer.add( WIDGET )      then:   "the total reflects the product's price"      totalizer.total ==  WIDGET .price    } }
assertEquals  "Expo '86" , album.title assertFalse album.isCompilation() assertNotNull album.tracks.find { it ==  "Palm Road"  } JUnit 3… assertThat album.title, equalTo( "Expo '86" ) assertThat album.isCompilation(), is(false) assertThat album.tracks, hasItem( "Palm Road" ) JUnit 4… then: album.title ==  "Expo '86" !album.isCompilation() album.tracks.find { it ==  "Palm Road"  } Spock…
Condition not satisfied: starship.speed == old(starship.speed) + warpFactor |  |  |  |  | | |  8  |  5  7 2 |  false [USS Enterprise NCC-1701] at StarshipSpec.starship accelerates predictably(StarshipSpec.groovy:44)
@Unroll( &quot;adding a  #product  increments the total&quot; ) def  &quot;adding a product increments the total&quot; () {    when:   totalizer.add(product)    then: totalizer.total == product.price    where:   product << [ WIDGET ,  GIZMO ,  THINGY ] }
def  &quot;a user can be retrieved using their id&quot; () {    when:   def user = registry.findUser(id)    then:   user.name == name user.email == email    where:   id | name  | email 1   |  &quot;Rob Fletcher&quot;  |  &quot;rob@energizedwork.com&quot;   2   |  &quot;Enoch Root&quot;   |  &quot;root@eruditorum.org&quot;   3   |  &quot;Edward Teach&quot;  |  &quot;blackbeard@providence.bs&quot;   }
def  &quot;a message is sent when someone registers&quot; () {    given:    def registry = new Registry()    def mailer =  Mock(Mailer)    registry.mailer = mailer    when:    registry.addPerson( &quot;Rob Fletcher&quot; ,  &quot;rob@ew.com&quot; )    then:    1 *  mailer.send( &quot;rob@ew.com&quot; ,  &quot;Welcome Rob!&quot; ) }
import  grails.plugin.spock.* class UserSpec extends  UnitSpec  { def  &quot;users can be saved and retrieved&quot; () { setup:  mockDomain(User) when: def user = new User(name:  &quot;Rob&quot; ).save() then:  !user.hasErrors() and:  User.findByName( &quot;Rob&quot; ) } }
def  &quot;Login fails if incorrect details submitted&quot; () { when:   &quot;a user visits the login page&quot; def loginPage = LoginPage.open() and:   &quot;enters their details incorrectly&quot; loginPage.loginWith( &quot;blackbeard&quot; ,  &quot;badpassword&quot; ) then:   &quot;they should see an error message&quot; loginPage.loginError == LoginPage. LOGIN_FAILED_MSG and:   &quot;they should not be logged in&quot; !HomePage.open().isLoggedIn() }
import  geb.spock.* class GoogleSpockSpec extends  GebSpec  { def  &quot;perform search&quot; () { when: to GoogleHomePage search.search( &quot;spock framework&quot; ) then: at GoogleResultsPage resultLink(0).text() ==  &quot;spock - Project Hosting on Google Code&quot; } }
spockframework.org blog.spockframework.org grails.org/plugin/spock Energized Work: energizedwork.com Blog: adhockery.blogspot.com Twitter:  @rfletcherEW

Groovier testing with Spock

  • 1.
    Rob Fletcher gitclone http://github.com/robfletcher/ggug-spock-examples
  • 2.
    Expressive testing language Easy to learn Usable from unit to end-to-end level Leverages Groovy language features Runs with JUnit: compatible with IDEs, build tools & CI Extensible for specialized testing scenarios &quot;Everything is questioned, and only the essential is kept&quot;
  • 3.
    Classes extend spock.lang.Specification Feature methods: def &quot;name in quotes&quot;() separated into labelled blocks Lifecycle: setup cleanup setupSpec cleanupSpec
  • 4.
    given : preconditions, data fixtures, etc. when : actions that trigger some outcome then : makes assertions about outcome expect : short alternative to when & then where : applies varied inputs and : sub-divides other blocks setup : alias for given cleanup : post-conditions, housekeeping, etc.
  • 5.
    import spock.lang.* classTotalizerSpec extends Specification {    def totalizer = new Totalizer()    def &quot;adding an item increments the total&quot; () {      when: &quot;a product is added&quot;      totalizer.add( WIDGET )      then: &quot;the total reflects the product's price&quot;      totalizer.total == WIDGET .price    } }
  • 6.
    assertEquals &quot;Expo'86&quot; , album.title assertFalse album.isCompilation() assertNotNull album.tracks.find { it == &quot;Palm Road&quot; } JUnit 3… assertThat album.title, equalTo( &quot;Expo '86&quot; ) assertThat album.isCompilation(), is(false) assertThat album.tracks, hasItem( &quot;Palm Road&quot; ) JUnit 4… then: album.title == &quot;Expo '86&quot; !album.isCompilation() album.tracks.find { it == &quot;Palm Road&quot; } Spock…
  • 7.
    Condition not satisfied:starship.speed == old(starship.speed) + warpFactor | | | | | | | 8 | 5 7 2 | false [USS Enterprise NCC-1701] at StarshipSpec.starship accelerates predictably(StarshipSpec.groovy:44)
  • 8.
    @Unroll( &quot;adding a #product increments the total&quot; ) def &quot;adding a product increments the total&quot; () {    when: totalizer.add(product)    then: totalizer.total == product.price    where: product << [ WIDGET , GIZMO , THINGY ] }
  • 9.
    def &quot;auser can be retrieved using their id&quot; () {    when: def user = registry.findUser(id)    then: user.name == name user.email == email    where: id | name | email 1 | &quot;Rob Fletcher&quot; | &quot;rob@energizedwork.com&quot; 2 | &quot;Enoch Root&quot; | &quot;root@eruditorum.org&quot; 3 | &quot;Edward Teach&quot; | &quot;blackbeard@providence.bs&quot; }
  • 10.
    def &quot;amessage is sent when someone registers&quot; () {    given:    def registry = new Registry()    def mailer = Mock(Mailer)    registry.mailer = mailer    when:    registry.addPerson( &quot;Rob Fletcher&quot; , &quot;rob@ew.com&quot; )    then:    1 * mailer.send( &quot;rob@ew.com&quot; , &quot;Welcome Rob!&quot; ) }
  • 11.
    import grails.plugin.spock.*class UserSpec extends UnitSpec { def &quot;users can be saved and retrieved&quot; () { setup: mockDomain(User) when: def user = new User(name: &quot;Rob&quot; ).save() then: !user.hasErrors() and: User.findByName( &quot;Rob&quot; ) } }
  • 12.
    def &quot;Loginfails if incorrect details submitted&quot; () { when: &quot;a user visits the login page&quot; def loginPage = LoginPage.open() and: &quot;enters their details incorrectly&quot; loginPage.loginWith( &quot;blackbeard&quot; , &quot;badpassword&quot; ) then: &quot;they should see an error message&quot; loginPage.loginError == LoginPage. LOGIN_FAILED_MSG and: &quot;they should not be logged in&quot; !HomePage.open().isLoggedIn() }
  • 13.
    import geb.spock.*class GoogleSpockSpec extends GebSpec { def &quot;perform search&quot; () { when: to GoogleHomePage search.search( &quot;spock framework&quot; ) then: at GoogleResultsPage resultLink(0).text() == &quot;spock - Project Hosting on Google Code&quot; } }
  • 14.
    spockframework.org blog.spockframework.org grails.org/plugin/spockEnergized Work: energizedwork.com Blog: adhockery.blogspot.com Twitter: @rfletcherEW

Editor's Notes