The Spock unit testing framework is on the verge of a 1.0 release and has already proven itself to be the next generation thinking on how to test Java production code. One of the many ever present challenges to testing code is the ability to Mock classes which has simplified by Spock from a very early release. Recently added to Spock is the notion of Stubs and Spies. This sessions is designed to demonstrate proper unit testing technique showing off these new features along with a number of advanced Spock features.
18. Why use Spock?
Spock Testing
n Expressive
testing language
n Easy to learn
n Usable from unit to end-to-end
n Leverages Groovy
n Runs with JUnit Runner
¨IDE
¨CI
18
21. Terms
Spock Testing
n Specification
¨compare
to TestCase or GroovyTestCase
¨Instructs JUnit to run with Sputnik (JUnit runner)
n Fields
¨initialized
for each “test”
¨think “setup”
¨not shared between feature methods
21
24. Feature Methods
Spock Testing
n “heart”
of spec
n four phases
¨setup
the features fixture
¨provide stimulus to system
¨describes the response
¨clean up
24
25. Blocks
Spock Testing
given:
preconditions, data fixtures
when:
actions that trigger some outcome
then:
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
25
28. When / Then Blocks
n used
together
¨ possible
n then
Spock Testing
to have many per feature
restrictions
¨ conditions
¨ exception
conditions
¨ automatic asserts
¨ interactions
¨ variable defs
28
47. Summary
n
Closing and Q&A
¨ Please
fill out the session evaluation
¨ Ken Sipe
ken.sipe@gmail.com
n kensipe.blogspot.com
n twitter: @kensipe
n
Spock Testing