Your SlideShare is downloading. ×
Spock
About me●   Software developer/architect at Michigan    State University●   Before that, 7 years in the insurance industry...
Executable specifications
●   Natural language●   Programmer oriented
BDD
● Extension of TDD (TDD done right!)● Organized into scenarios: given an initial  context, when an event occurs, then ensu...
●   Tools    ●   Cucumber    ●   EasyB    ●   JBehave
Spock
● Spock is a BDD framework written in Groovy● Based on tools and languages like JUnit,  jMock, Mockito, Groovy, Scala● Cur...
●   Concise●   Same tools●   Natural language●   Powerful
Getting ready
● http://code.google.  com/p/spock/wiki/GettingStarted● Instructions for Eclipse, IntelliJ, NetBeans
Spock Web Console
http://meetspock.appspot.com/
Spock and your IDE
●   For Eclipse: http://dist.springsource.    org/release/GRECLIPSE/e4.2/
Spock Terminology                 Spock                       JUnitSpecification            Test●    JUnit                ...
Anatomy of a SpockSpecification
Spock specifications extendspock.lang.Specification
●   Fields●   Fixture methods●   Feature methods●   Helper methods
The basics
Spock uses the built in JUnitrunner in your IDE
Executable specifications – Now we’re talking!
What happens whena test fails?
How can we verify that anexception isn’t thrown?
Conditions
Types●   Implicit    ●   Statements in then: and expect: blocks●   Explicit    ●   Denoted by assert keyword elsewhere●   ...
Feature Methods●   When – Then●   When – Then – Where●   Given – When – Then●   Expect●   Expect – Where
Expect:
Stimulus and response are the same statement.
Data Driven Testing
If my test passes, that’s great!But what if it fails???
I can easily see which rowsin the table didn’t pass.
Another example
Column NameTable values
Now, I can compare each result with eachrow in the table in my test.
And I can see individual row results
Interactions (Mocking)
●   Spock has its own mocking framework built in●   Can mock interfaces and classes●   Cannot do partial mocks (as of righ...
Another example
Behavior for an invalid appointmentBehavior when the appointment is on a holiday
Database Interactions
● Specifications that access a database can be  mocked.   ○ Can use in-memory databases like H2 or Derby● Use shared annot...
Constraints on return values
subscriber.isAlive() >> true           // single return value,  repeated indefinitelysubscriber.isAlive() >>> [true, false...
Helper methods
Helper Method Requirements● Implicit conditions must be turned into  explicit conditions (use assert keyword)● Must have v...
Spock extensions
●   Interception-based●   Built-in    ●   @Timeout    ●   @Ignore    ●   @IgnoreRest    ●   @FailsWith●   Custom
Spock and Geb
Geb● Browser automation● Uses WebDriver(Selenium) and HtmlUnit● Built-in support for PageObject Pattern
What are page objects?Models of areas of your UI that your tests interact with.
For BDD testing youll need Geb, Geb for Spock, and Selenium.
Testing with Spock andFrameworks
●   Spring:    http://code.google.com/p/spock/wiki/SpringExtension●   Grails:    http://grails.org/doc/2.0.0.RC1/guide/tes...
Summary
● Executable Specifications● BDD● Spock  ○ Feature methods
●   Given-Stimulus-Response    ○   When-Then    ○   Given-When-Then    ○   Expect-Where    ○   etc.
● Spock can integrate with a number of  frameworks  ○ Geb  ○ Spring  ○ Grails
Links● Spock Framework: http://code.google.  com/p/spock/● Spock basics: http://code.google.  com/p/spock/wiki/SpockBasics...
Live long and     happy testing!
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Spock pres
Upcoming SlideShare
Loading in...5
×

Spock pres

698

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
698
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Spock pres"

  1. 1. Spock
  2. 2. About me● Software developer/architect at Michigan State University● Before that, 7 years in the insurance industry as a software developer.● Twitter: @elizhender
  3. 3. Executable specifications
  4. 4. ● Natural language● Programmer oriented
  5. 5. BDD
  6. 6. ● Extension of TDD (TDD done right!)● Organized into scenarios: given an initial context, when an event occurs, then ensure some outcome● Spock borrows from BDD extensively
  7. 7. ● Tools ● Cucumber ● EasyB ● JBehave
  8. 8. Spock
  9. 9. ● Spock is a BDD framework written in Groovy● Based on tools and languages like JUnit, jMock, Mockito, Groovy, Scala● Current version: 0.7 - Released in early October● Yes, it is named after the character on Star Trek!
  10. 10. ● Concise● Same tools● Natural language● Powerful
  11. 11. Getting ready
  12. 12. ● http://code.google. com/p/spock/wiki/GettingStarted● Instructions for Eclipse, IntelliJ, NetBeans
  13. 13. Spock Web Console
  14. 14. http://meetspock.appspot.com/
  15. 15. Spock and your IDE
  16. 16. ● For Eclipse: http://dist.springsource. org/release/GRECLIPSE/e4.2/
  17. 17. Spock Terminology Spock JUnitSpecification Test● JUnit classsetup() @Beforecleanup() @AftersetupSpec() @BeforeClasscleanupSpec() @AfterClassFeature TestParameterized feature TheoryCondition AssertionException condition @Test(expected=…)@FailsWith @Test(expected=…)Interaction Mock expectation (JMock, EasyMock, Mockito)
  18. 18. Anatomy of a SpockSpecification
  19. 19. Spock specifications extendspock.lang.Specification
  20. 20. ● Fields● Fixture methods● Feature methods● Helper methods
  21. 21. The basics
  22. 22. Spock uses the built in JUnitrunner in your IDE
  23. 23. Executable specifications – Now we’re talking!
  24. 24. What happens whena test fails?
  25. 25. How can we verify that anexception isn’t thrown?
  26. 26. Conditions
  27. 27. Types● Implicit ● Statements in then: and expect: blocks● Explicit ● Denoted by assert keyword elsewhere● Exception ● thrown ● notThrown
  28. 28. Feature Methods● When – Then● When – Then – Where● Given – When – Then● Expect● Expect – Where
  29. 29. Expect:
  30. 30. Stimulus and response are the same statement.
  31. 31. Data Driven Testing
  32. 32. If my test passes, that’s great!But what if it fails???
  33. 33. I can easily see which rowsin the table didn’t pass.
  34. 34. Another example
  35. 35. Column NameTable values
  36. 36. Now, I can compare each result with eachrow in the table in my test.
  37. 37. And I can see individual row results
  38. 38. Interactions (Mocking)
  39. 39. ● Spock has its own mocking framework built in● Can mock interfaces and classes● Cannot do partial mocks (as of right now)
  40. 40. Another example
  41. 41. Behavior for an invalid appointmentBehavior when the appointment is on a holiday
  42. 42. Database Interactions
  43. 43. ● Specifications that access a database can be mocked. ○ Can use in-memory databases like H2 or Derby● Use shared annotation for the field with the driver information.
  44. 44. Constraints on return values
  45. 45. subscriber.isAlive() >> true // single return value, repeated indefinitelysubscriber.isAlive() >>> [true, false, true] // multiple return values (anything that Groovy can iterate over), last one repeated indefinitelydef random = new Random()subscriber.isAlive() >> { random.nextBoolean() } // custom return valuesubscriber.isAlive() >> { throw new TimeoutException() } // custom action
  46. 46. Helper methods
  47. 47. Helper Method Requirements● Implicit conditions must be turned into explicit conditions (use assert keyword)● Must have void return type
  48. 48. Spock extensions
  49. 49. ● Interception-based● Built-in ● @Timeout ● @Ignore ● @IgnoreRest ● @FailsWith● Custom
  50. 50. Spock and Geb
  51. 51. Geb● Browser automation● Uses WebDriver(Selenium) and HtmlUnit● Built-in support for PageObject Pattern
  52. 52. What are page objects?Models of areas of your UI that your tests interact with.
  53. 53. For BDD testing youll need Geb, Geb for Spock, and Selenium.
  54. 54. Testing with Spock andFrameworks
  55. 55. ● Spring: http://code.google.com/p/spock/wiki/SpringExtension● Grails: http://grails.org/doc/2.0.0.RC1/guide/testing.html
  56. 56. Summary
  57. 57. ● Executable Specifications● BDD● Spock ○ Feature methods
  58. 58. ● Given-Stimulus-Response ○ When-Then ○ Given-When-Then ○ Expect-Where ○ etc.
  59. 59. ● Spock can integrate with a number of frameworks ○ Geb ○ Spring ○ Grails
  60. 60. Links● Spock Framework: http://code.google. com/p/spock/● Spock basics: http://code.google. com/p/spock/wiki/SpockBasics● Geb: http://www.gebish.org/ ○ Manual: http://www.gebish.org/manual/0.7.0/index. html● BDD: http://behaviour-driven.org/
  61. 61. Live long and happy testing!

×