Your SlideShare is downloading. ×
0
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Groovier testing with Spock
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Groovier testing with Spock

10,826

Published on

Talk given at the London Groovy & Grails User Group by Rob Fletcher of Energized Work on 2010-08-16

Talk given at the London Groovy & Grails User Group by Rob Fletcher of Energized Work on 2010-08-16

0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
10,826
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
107
Comments
0
Likes
17
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • HAI
  • Transcript

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

    ×