The document discusses unit testing with the Spock testing framework. It introduces Spock and outlines key concepts like the three pillars of good unit tests, Spock idioms, mocks and stubs, and continuous integration. Spock provides an expressive testing language based on Groovy that makes tests more readable, maintainable and trustworthy. Examples demonstrate how to write feature methods, use blocks, verify interactions, stub implementations, test exceptions, integrate with Spring, and perform HTTP calls and data-driven testing with Spock.
2. AgendaAgenda
Three qualities of good unit test
Introduction to Spock
Spock Idioms
Mocks and Stubs
Continuous Integration Pipeline
Code coverage analysis
3. Why Unit testsWhy Unit tests
• Catch mistakes and avoid reworks
Another production issue
5. Why Unit testsWhy Unit tests
•Increased Productivity (shipped product)
•Cheaper to solve problems
6. Trustworthiness - trustworthy tests don’t have bugs
and they test the right things. They don’t “cry wolf”
3 Pillars of Good3 Pillars of Good
Unit TestUnit Test
7. Trustworthiness - trustworthy tests don’t have bugs
and they test the right things. They don’t “cry wolf”
Maintablility - Developers will simply stop maintaining
and fixing tests that takes too long to change
3 Pillars of Good3 Pillars of Good
Unit TestUnit Test
8. Trustworthiness - trustworthy tests don’t have bugs
and they test the right things. They don’t “cry wolf”
Maintablility - Developers will simply stop maintaining
and fixing tests that takes too long to change
Readability - this means not just be able to read tests
but also figuring out the problem if tests seems to be
wrong. Without readability, other 2 pillars fall pretty
quickly.
3 Pillars of Good3 Pillars of Good
Unit TestUnit Test
10. Spock FrameworkSpock Framework
Leverage Groovy
Expressive testing language
Easy to Learn
Reduce the line of test code
Productivity
Structural blocks - BDD style -context, stimulus,
expectation
11. First SpecFirst Spec
import spock.lang.Specification
class GivenWhenThenSpec extends Specification {
def "test adding a new item to a set"() {
given:
def bag = [4, 6, 3, 2] as Set
when:
bag << 1
then:
bag.size() == 5
12. Feature methodFeature method
// feature method
def “test adding a new item to a set” () {
// block goes here
}
heart of spec
Four phases
setup the features fixture
provide stimulus to the system
describe the response
clean up
13. BlocksBlocks
given: precondition, data fixture
when: actions that trigger some outcome
then: makes assertions about outcode
expect: short alt to when & then
where: applies varied inputs
setup: alias for given
and: sub-divided other blocks
cleanup: post-condition, cleanup
15. Mock and InteractionMock and Interaction
verificationverification
Spock has build-in support for mocking
Subscriber sub=Mock() or
def sub=Mock(Subscriber)
DSL for specifying interaction
2 * sub.receive(“msg”)
16. CP DEMO InteractionCP DEMO Interaction
EventController - captureEvent (Tactic dismissal for
IPP event)
Test class EventControllerTest
1 * userTacticRepository.deactivateTactic(_,_,_)
25. There is moreThere is more
@Fast - @Slow
@AutoCleanUp - clean up/close resources
Async testing support
Data Driven for parametirized tests
Get it from Maven Central 07-groovy-2.0
https://code.google.com/p/spock/
26. Comprehensive ApproachComprehensive Approach
to Improve Qualityto Improve Quality
Automated Unit Tests
Automated Acceptance Test
Continuous Integration
Code quality analysis
Measurable values: number of defects and rework