This document introduces the Spock testing framework as an alternative to JUnit for Java applications. Some key points:
- Spock uses Groovy's more succinct syntax to write tests, making them easier to write and read compared to JUnit. It supports features like state-driven testing, data-driven testing, mocking, and stubbing out of the box.
- Spock tests are fully compatible with JUnit so they can be run with existing tools. The framework also has many extensions for additional functionality and integrations.
- Benefits of Spock include easier testing, better test structure, easier understanding of failures, and support for non-technical stakeholders. While there is a learning curve, its
3. A Test is code that uses your
production code
3
4. A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
4
5. A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
• Design
5
6. A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
• Design
• Documentation
6
7. It does not matter how intelligent you
are, if you guess and that guess
cannot be backed up by experimental
evidence, then it is still a guess.
Richard Feynman (1964)
Correctness
7
8. A Software system can be best designed if
the testing is interlaced with the design
instead of being used after the design
Alan J. Perlis (1968)
Design
8
10. If JUnit was good enough for my
grandparents then why not for me?
• Written in Java: Statically typed, Rigid syntax, Hard
for DSLs
• Limited in its features, Requires “add-ons”:
Hamcrest, JAssert, JMock, Mockito, EasyMock,
PowerMock, JMockit, Cucumber, JBehave…
10
11. What is Spock?
• A Developer testing framework for the JVM (since 2008)
• Based on Groovy
• Succinct syntax and DSL built for tests
• Mocking/stubbing is already included
• BDD friendly
• Fully compatible with JUnit
11
54. Under the hood
• It’s Groovy!
• It uses Groovy AST Transformations
• It’s a JUnitRunner -> IDE Support, Build Tools
Support… anything that supports JUnit also
supports Spock
54
55. Wow!
• Cost of context switch - Legacy tests
• Cost of learning new DSL - “Legacy” developers
So, is there any reason why NOT to start using
Spock?
55
56. Use Spock
• Easier to write
• Easier to read
• Easier understanding of failures
• Promotes good structured tests
• Allows non technical people to be involved
• Easy to extend
56