java testing with spock - ken sipe (trexin consulting)
DESCRIPTION
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.TRANSCRIPT
NFJS Software Symposium Series 2013
Ken Sipe
Spock - The Logical Enterprise Testing Tool
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]
Spock TestingAgenda
nTestingnIntroduction to SpocknMocking
Spock Testing
Spock Intro
4
Spock Testing
ntesting framework...
nbased on Groovy
nfully compatible with JUnit
nresult of learnings from¨RSpec, BDD, JUnit
5
Spock is...
Spock TestingSpock Can...
nReduces lines of code
nMake tests more readable
nBe extended
6
Spock Testing
JUnit
7
Spock TestingTaxonomy of JUnit
Spock Testing
9
JUnit Pain Points?
Spock TestingUnit testing needs
nConcise, Clear and ReadablenPromote “user” thinking
¨context¨stimulus¨expectations
nProductivity
10
Spock Testing
Groovy
11
Spock TestingTaxonomy of GroovyTestCase
Spock TestingGroovy and Testing
Spock TestingGroovy Power Assert
Condition not satisfied:
interest == calc.calculate(amt, year)| | | | | |
25.0 | | 20.0 100 4 | com.math.SimpleInterestCalculator@606e1dec
false
Spock TestingGroovy Improvements
nConcise, Clear and ReadablenPromote “user” thinking
¨context¨stimulus¨expectations
nProductivity
15
Spock Testing
Spock
16
Spock TestingSpock
nProgrammers Environment¨Groovy
nPromotes Clarity¨structural blocks¨removes noise
17
Spock TestingWhy use Spock?
nExpressive testing languagenEasy to learnnUsable from unit to end-to-endnLeverages GroovynRuns with JUnit Runner
¨IDE¨CI
18
Spock Testing
Taxonomy of a Spec
19
Spock TestingFirst Spec
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
Spock TestingMore Fields
nShared¨Setup once¨think setupSpec()
nstatics¨only use for constants
22
Spock TestingFixture Methods
nbefore / after a featurenbefore / after a specnoptional
23
Spock TestingFeature Methods
n“heart” of specnfour phases
¨setup the features fixture¨provide stimulus to system¨describes the response¨clean up
24
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
Spock TestingCommon Blocks
nWhen / Then / WherenGiven / When / Then
26
Spock TestingBlocks
nsetup
¨must be first¨must be the only¨no special semantics¨label is optional¨label given: is an alias
27
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
Spock TestingConditions
nchecking for exceptions
29
Spock TestingThrowing an Exception NOT!
30
Spock TestingMocks and Expectations
31
Spock TestingCleanup on Feature X
ncleanup block¨only followed by a where block¨no repeats
32
Spock TestingWhere Blocks
¨last in a method¨no repeats¨used for data-driven features
33
Spock TestingHelper Methods
¨either return a booleannor
¨assert34
Spock Testing
Specification Functionsand Spock.lang.*
35
Spock Testing
nold()nthrown() / notThrownnwith {}
36
Spock TestingSpock.lang.* Documentation
37
@Title@Narrative @Issue@See@Subject
Spock TestingSpock.lang.* Ignoring Stuff
38
@Requires @IgnoreIf@Ignore@IgnoreRest
Spock TestingReporting
39
@Unroll
Spock TestingBuilt-in Extensions: AutoCleanup
40
@AutoCleanup@AutoCleanup(‘dispose’)@AutoCleanup(quite=true)
Spock TestingBuilt-in Extensions: Timeout
41
@Timeout@Timeout(10)@Timeout(value=10, unit=TimeUnit.MILLISECONDS)
Spock TestingBuilt-in Extensions: Stepwise
42
@Stepwise
Spock Testing
Hamcrest
43
Spock Testing
JUnit Rules
44
Spock Testing
SpockConfig
45
Spock TestingReferences
46
nGetting Spock¨http://code.google.com/p/spock/
nSource from Presentation¨https://github.com/kensipe/spock-demos-nfjs
Spock Testing
n Closing and Q&A
¨Please fill out the session evaluation¨Ken Sipe
n [email protected] kensipe.blogspot.comn twitter: @kensipe
Summary