Spock - The Logical Enterprise Testing...
Transcript of Spock - The Logical Enterprise Testing...
![Page 1: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/1.jpg)
NFJS Software Symposium Series 2013
Ken Sipe
Spock - The Logical Enterprise Testing Tool
![Page 2: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/2.jpg)
Spock TestingAbout Speaker
Developer: Embedded, C++, Java, Groovy, Grails, C#, Objective CSpeaker: JavaOne 2009 Rock Star, NFJS, JAXMicrosoft MCPSun Certified Java 2 ArchitectMaster of Scrums Agile CoachInstructor: VisiBroker CORBA
Rational Rose, OOAD
http://kensipe.blogspot.com/http://del.icio.us/kensipetwitter: @[email protected]
![Page 3: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/3.jpg)
Spock TestingAgenda
nTestingnIntroduction to SpocknMocking
![Page 4: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/4.jpg)
Spock Testing
Spock Intro
4
![Page 5: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/5.jpg)
Spock Testing
ntesting framework...
nbased on Groovy
nfully compatible with JUnit
nresult of learnings from¨RSpec, BDD, JUnit
5
Spock is...
![Page 6: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/6.jpg)
Spock TestingSpock Can...
nReduces lines of code
nMake tests more readable
nBe extended
6
![Page 7: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/7.jpg)
Spock Testing
JUnit
7
![Page 8: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/8.jpg)
Spock TestingTaxonomy of JUnit
![Page 9: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/9.jpg)
Spock Testing
9
JUnit Pain Points?
![Page 10: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/10.jpg)
Spock TestingUnit testing needs
nConcise, Clear and ReadablenPromote “user” thinking
¨context¨stimulus¨expectations
nProductivity
10
![Page 11: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/11.jpg)
Spock Testing
Groovy
11
![Page 12: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/12.jpg)
Spock TestingTaxonomy of GroovyTestCase
![Page 13: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/13.jpg)
Spock TestingGroovy and Testing
![Page 14: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/14.jpg)
Spock TestingGroovy Power Assert
Condition not satisfied:
interest == calc.calculate(amt, year)| | | | | |
25.0 | | 20.0 100 4 | com.math.SimpleInterestCalculator@606e1dec
false
![Page 15: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/15.jpg)
Spock TestingGroovy Improvements
nConcise, Clear and ReadablenPromote “user” thinking
¨context¨stimulus¨expectations
nProductivity
15
![Page 16: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/16.jpg)
Spock Testing
Spock
16
![Page 17: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/17.jpg)
Spock TestingSpock
nProgrammers Environment¨Groovy
nPromotes Clarity¨structural blocks¨removes noise
17
![Page 18: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/18.jpg)
Spock TestingWhy use Spock?
nExpressive testing languagenEasy to learnnUsable from unit to end-to-endnLeverages GroovynRuns with JUnit Runner
¨IDE¨CI
18
![Page 19: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/19.jpg)
Spock Testing
Taxonomy of a Spec
19
![Page 20: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/20.jpg)
Spock TestingFirst Spec
![Page 21: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/21.jpg)
Spock TestingTerms
nSpecification¨compare to TestCase or GroovyTestCase¨Instructs JUnit to run with Sputnik (JUnit runner)
nFields¨initialized for each “test”¨think “setup”¨not shared between feature methods
21
![Page 22: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/22.jpg)
Spock TestingMore Fields
nShared¨Setup once¨think setupSpec()
nstatics¨only use for constants
22
![Page 23: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/23.jpg)
Spock TestingFixture Methods
nbefore / after a featurenbefore / after a specnoptional
23
![Page 24: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/24.jpg)
Spock TestingFeature Methods
n“heart” of specnfour phases
¨setup the features fixture¨provide stimulus to system¨describes the response¨clean up
24
![Page 25: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/25.jpg)
Spock TestingBlocks
25
given: preconditions, data fixtures
when: actions that trigger some outcomethen: makes assertions about outcome
expect: short alt to when & then
where: applies varied inputs
and: sub-divides other blocks
setup: alias for given
cleanup: post-conditions, housekeeping
![Page 26: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/26.jpg)
Spock TestingCommon Blocks
nWhen / Then / WherenGiven / When / Then
26
![Page 27: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/27.jpg)
Spock TestingBlocks
nsetup
¨must be first¨must be the only¨no special semantics¨label is optional¨label given: is an alias
27
![Page 28: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/28.jpg)
Spock TestingWhen / Then Blocks
n used together¨possible to have many per feature
n then restrictions¨conditions¨exception conditions¨automatic asserts¨interactions¨variable defs
28
![Page 29: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/29.jpg)
Spock TestingConditions
nchecking for exceptions
29
![Page 30: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/30.jpg)
Spock TestingThrowing an Exception NOT!
30
![Page 31: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/31.jpg)
Spock TestingMocks and Expectations
31
![Page 32: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/32.jpg)
Spock TestingCleanup on Feature X
ncleanup block¨only followed by a where block¨no repeats
32
![Page 33: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/33.jpg)
Spock TestingWhere Blocks
¨last in a method¨no repeats¨used for data-driven features
33
![Page 34: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/34.jpg)
Spock TestingHelper Methods
¨either return a booleannor
¨assert34
![Page 35: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/35.jpg)
Spock Testing
Specification Functionsand Spock.lang.*
35
![Page 36: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/36.jpg)
Spock Testing
nold()nthrown() / notThrownnwith {}
36
![Page 37: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/37.jpg)
Spock TestingSpock.lang.* Documentation
37
@Title@Narrative @Issue@See@Subject
![Page 38: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/38.jpg)
Spock TestingSpock.lang.* Ignoring Stuff
38
@Requires @IgnoreIf@Ignore@IgnoreRest
![Page 39: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/39.jpg)
Spock TestingReporting
39
@Unroll
![Page 40: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/40.jpg)
Spock TestingBuilt-in Extensions: AutoCleanup
40
@AutoCleanup@AutoCleanup(‘dispose’)@AutoCleanup(quite=true)
![Page 41: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/41.jpg)
Spock TestingBuilt-in Extensions: Timeout
41
@Timeout@Timeout(10)@Timeout(value=10, unit=TimeUnit.MILLISECONDS)
![Page 42: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/42.jpg)
Spock TestingBuilt-in Extensions: Stepwise
42
@Stepwise
![Page 43: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/43.jpg)
Spock Testing
Hamcrest
43
![Page 44: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/44.jpg)
Spock Testing
JUnit Rules
44
![Page 45: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/45.jpg)
Spock Testing
SpockConfig
45
![Page 46: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/46.jpg)
Spock TestingReferences
46
nGetting Spock¨http://code.google.com/p/spock/
nSource from Presentation¨https://github.com/kensipe/spock-demos-nfjs
![Page 47: Spock - The Logical Enterprise Testing Tools3-eu-west-1.amazonaws.com/presentations2013/63_presentation.pdfSpock Testing!testing framework...!based on Groovy!fully compatible with](https://reader034.fdocuments.in/reader034/viewer/2022042322/5f0d00d97e708231d4383237/html5/thumbnails/47.jpg)
Spock Testing
n Closing and Q&A
¨Please fill out the session evaluation¨Ken Sipe
n [email protected] kensipe.blogspot.comn twitter: @kensipe
Summary