NFJS Software Symposium Series 2013

Spock - The Logical Enterprise
Testing Tool
Ken Sipe
About Speaker

Spock Testing

http://kensipe.blogspot.com/
http://del.icio.us/kensipe
twitter: @kensipe
ken.sipe@gmail.com

Developer: Embedded, C++, Java, Groovy, Grails, C#, Objective C
Speaker: JavaOne 2009 Rock Star, NFJS, JAX
Microsoft MCP
Sun Certified Java 2 Architect
Master of Scrums
Agile Coach
Instructor: VisiBroker CORBA
Rational Rose, OOAD
Agenda

Spock Testing

n Testing
n Introduction
n Mocking

to Spock
Spock Testing

Spock Intro

4
Spock is...
n testing

framework...

n based

Spock Testing

on Groovy

n fully

compatible with JUnit

n result

of learnings from

¨RSpec,

BDD, JUnit

5
Spock Can...
n Reduces
n Make
n Be

Spock Testing

lines of code

tests more readable

extended

6
Spock Testing

JUnit

7
Taxonomy of JUnit

Spock Testing
Spock Testing

JUnit Pain Points?

9
Unit testing needs

Spock Testing

n Concise,

Clear and Readable
n Promote “user” thinking
¨context
¨stimulus
¨expectations

n Productivity

10
Spock Testing

Groovy

11
Taxonomy of GroovyTestCase

Spock Testing
Groovy and Testing

Spock Testing
Groovy Power Assert

Condition not satisfied:
interest == calc.calculate(amt, year)
|
| |
|
| |
25.0 | |
20.0 100 4
| com.math.SimpleInterestCalculator@606e1dec
false

Spock Testing
Groovy Improvements

Spock Testing

n Concise,

Clear and Readable
n Promote “user” thinking
¨context
¨stimulus
¨expectations

n Productivity

15
Spock Testing

Spock

16
Spock

Spock Testing

n Programmers

Environment

¨Groovy

n Promotes

Clarity

¨structural

blocks
¨removes noise

17
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
Spock Testing

Taxonomy of a Spec

19
First Spec

Spock Testing
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
More Fields

Spock Testing

n Shared
¨Setup

once
¨think setupSpec()
n statics
¨only

use for constants

22
Fixture Methods

Spock Testing

n before

/ after a feature
n before / after a spec
n optional

23
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
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
Common Blocks

Spock Testing

n When

/ Then / Where
n Given / When / Then

26
Blocks

Spock Testing

n setup
¨must

be first
¨must be the only
¨no special semantics
¨label is optional
¨label given: is an alias

27
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
Conditions

n checking

Spock Testing

for exceptions

29
Throwing an Exception NOT!

Spock Testing

30
Mocks and Expectations

Spock Testing

31
Cleanup on Feature X

n cleanup

Spock Testing

block

¨only

followed by a where block
¨no repeats

32
Where Blocks

Spock Testing

¨last

in a method
¨no repeats
¨used for data-driven features

33
Helper Methods

¨either

Spock Testing

return a boolean

n or

¨assert
34
Spock Testing

Specification Functions
and Spock.lang.*

35
Spock Testing

n old()
n thrown()
n with

/ notThrown

{}

36
Spock.lang.* Documentation

Spock Testing

@Title
@Narrative
@Issue
@See
@Subject

37
Spock.lang.* Ignoring Stuff

Spock Testing

@Requires
@IgnoreIf
@Ignore
@IgnoreRest

38
Reporting

Spock Testing

@Unroll

39
Built-in Extensions: AutoCleanup

Spock Testing

@AutoCleanup
@AutoCleanup(‘dispose’)
@AutoCleanup(quite=true)

40
Built-in Extensions: Timeout

Spock Testing

@Timeout
@Timeout(10)
@Timeout(value=10,
unit=TimeUnit.MILLISECONDS)

41
Built-in Extensions: Stepwise

Spock Testing

@Stepwise

42
Spock Testing

Hamcrest

43
Spock Testing

JUnit Rules

44
Spock Testing

SpockConfig

45
References
n Getting

Spock Testing

Spock

¨http://code.google.com/p/spock/

n Source

from Presentation

¨https://github.com/kensipe/spock-demos-nfjs

46
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

Java Testing With Spock - Ken Sipe (Trexin Consulting)