Slide 1

Slide 1 text

SMARTER TESTING WITH SPOCK Baruch Sadogursky by Peter Niederwieser

Slide 2

Slide 2 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 3

Slide 3 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 4

Slide 4 text

The creator of Spock Groovy & Gradle committer Works for Gradleware @pniederw 4 Peter Niederwieser

Slide 5

Slide 5 text

Developer Advocate at JFrog Loves Gr… things @jbaruch 5 Baruch Sadogursky

Slide 6

Slide 6 text

WHAT IS SPOCK?

Slide 7

Slide 7 text

A developer testing framework... Spock is...

Slide 8

Slide 8 text

A developer testing framework... for Groovy and Java applications... Spock is...

Slide 9

Slide 9 text

A developer testing framework... for Groovy and Java applications... based on Groovy... Spock is...

Slide 10

Slide 10 text

A developer testing framework... for Groovy and Java applications... based on Groovy... fully compatible with JUnit... Spock is...

Slide 11

Slide 11 text

A developer testing framework... for Groovy and Java applications... based on Groovy... fully compatible with JUnit... but going beyond! Spock is...

Slide 12

Slide 12 text

Reduce the lines of test code Spock Can...

Slide 13

Slide 13 text

Reduce the lines of test code Make tests more readable Spock Can...

Slide 14

Slide 14 text

Reduce the lines of test code Make tests more readable Turn tests into specifications Spock Can...

Slide 15

Slide 15 text

Reduce the lines of test code Make tests more readable Turn tests into specifications Be extended in powerful ways Spock Can...

Slide 16

Slide 16 text

Reduce the lines of test code Make tests more readable Turn tests into specifications Be extended in powerful ways Bring back the fun to testing! Spock Can...

Slide 17

Slide 17 text

Homepage http://spockframework.org Source Code http://github.spockframework.org/spock Documentation http://docs.spockframework.org/en/latest Spock Web Console http://webconsole.spockframework.org Spock Example Project http://github.spockframework.org/spock-example Slides and Code for this Presentation http://github.spockframework.org/smarter-testing-with-spock Getting Started

Slide 18

Slide 18 text

Who’s Using Spock?

Slide 19

Slide 19 text

Who’s Using Spock? (2)

Slide 20

Slide 20 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 21

Slide 21 text

Unit State Testing Vocabulary 21 Given some state of some objects… We write We mean Setup Given

Slide 22

Slide 22 text

Unit State Testing Vocabulary 22 Given some state of some objects… When we change that state… We write We mean Setup Given Change state When

Slide 23

Slide 23 text

Unit State Testing Vocabulary 23 Given some state of some objects… When we change that state… Then the results should be… We write We mean Setup Given Change state When Assert Then

Slide 24

Slide 24 text

Unit State Testing Vocabulary 24 We write We mean Setup Given Change state When Assert Then

Slide 25

Slide 25 text

 Blocks setup: cleanup: expect: given: when: then: where: and:  Fixture Methods setup() cleanup() setupSpec() cleanupSpec()  Instance and @Shared fields  old() and thrown()  Forces developers to think like users  Nice looking reports (that no one reads) Recap: State Based Testing

Slide 26

Slide 26 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 27

Slide 27 text

Test the same behavior... ...with varying data! Data Driven Testing

Slide 28

Slide 28 text

Test the same behavior... ...with varying data! Data Driven Testing

Slide 29

Slide 29 text

where: block Data tables External data sources @Unroll Recap: Data Driven Testing

Slide 30

Slide 30 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 31

Slide 31 text

How objects communicate Interaction Based Testing

Slide 32

Slide 32 text

How objects communicate Mocking frameworks simplify the job Interaction Based Testing

Slide 33

Slide 33 text

How objects communicate Mocking frameworks simplify the job Spock has its own mocking framework Interaction Based Testing

Slide 34

Slide 34 text

How objects communicate Mocking frameworks simplify the job Spock has its own mocking framework Google it: “Mocks aren't Stubs” Interaction Based Testing

Slide 35

Slide 35 text

How objects communicate Mocking frameworks simplify the job Spock has its own mocking framework Interaction Based Testing

Slide 36

Slide 36 text

Creating Recap: Interaction Based Testing def sub = Mock(Subscriber) Subscriber sub = Mock()

Slide 37

Slide 37 text

Creating Mocking Recap: Interaction Based Testing def sub = Mock(Subscriber) Subscriber sub = Mock() 1 * sub.receive("msg") (1..3) * sub.receive(_) (1.._) * sub.receive(_ as String) 1 * sub.receive(!null) 1 * sub.receive({it.contains("m")}) 1 * _./rec.*/("msg")

Slide 38

Slide 38 text

Stubbing Recap: Interaction Based Testing (2) // now returns status code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" }

Slide 39

Slide 39 text

Stubbing Mocking and Stubbing Recap: Interaction Based Testing (2) // now returns status code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" } 3 * sub.receive(_) >>> ["ok", "ok", "fail"]

Slide 40

Slide 40 text

Stubbing Mocking and Stubbing Impressing your friends Recap: Interaction Based Testing (2) // now returns status code String receive(String msg) { ... } sub.receive(_) >> "ok" sub.receive(_) >>> ["ok", "ok", "fail"] sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" } 3 * sub.receive(_) >>> ["ok", "ok", "fail"] (_.._) * _._(*_) >> _

Slide 41

Slide 41 text

NEW IN 0.7 What’s new?

Slide 42

Slide 42 text

Group conditions/interactions w/ same target New in 0.7 (1)

Slide 43

Slide 43 text

Group conditions/interactions w/ same target Better mocking failure messages New in 0.7 (1)

Slide 44

Slide 44 text

Stubs More at: http://docs.spockframework.org/en/latest/new_and_noteworthy.html New in 0.7 (2)

Slide 45

Slide 45 text

Stubs Spies More at: http://docs.spockframework.org/en/latest/new_and_noteworthy.html New in 0.7 (2)

Slide 46

Slide 46 text

Stubs Spies Groovy mocks More at: http://docs.spockframework.org/en/latest/new_and_noteworthy.html New in 0.7 (2)

Slide 47

Slide 47 text

Stubs Spies Groovy mocks Declare interactions upfront More at: http://docs.spockframework.org/en/latest/new_and_noteworthy.html New in 0.7 (2)

Slide 48

Slide 48 text

Stubs Spies Groovy mocks Declare interactions upfront More at: http://docs.spockframework.org/en/latest/new_and_noteworthy.html New in 0.7 (2)

Slide 49

Slide 49 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 50

Slide 50 text

Listeners Spock Extensions

Slide 51

Slide 51 text

Listeners Interceptors Spock Extensions

Slide 52

Slide 52 text

Listeners Interceptors Annotation-driven extensions Spock Extensions

Slide 53

Slide 53 text

Listeners Interceptors Annotation-driven extensions Global extensions Spock Extensions

Slide 54

Slide 54 text

Built-in Extensions @Ignore @IgnoreRest @FailsWith @Timeout @AutoCleanup @Stepwise @RevertMetaClass @Rule

Slide 55

Slide 55 text

Built-in Extensions @Ignore @IgnoreRest @FailsWith @Timeout @AutoCleanup @Stepwise @RevertMetaClass @Rule

Slide 56

Slide 56 text

External Extensions  spock-grails  spock-spring  spock-guice  spock-tapestry  spock-unitils  spock-griffon  spock-arquillian  spock-extensions http://github.com/robfletcher/spock-extensions

Slide 57

Slide 57 text

http://grails.org/plugin/spock http://github.spockframework.org/spock- grails grails install plugin spock 0.6 grails test-app grails test-app integration:spock ‘C*’ Grails Extension

Slide 58

Slide 58 text

Grails Extension (2)

Slide 59

Slide 59 text

Spring Extension

Slide 60

Slide 60 text

Meta & Intro State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff What we’ll talk about

Slide 61

Slide 61 text

~/.spock/SpockConfig.groovy, or on class path, or with -Dspock.configuration Configuring Spock

Slide 62

Slide 62 text

~/.spock/SpockConfig.groovy, or on class path, or with -Dspock.configuration Configuring Spock runner { filterStackTrace false include Fast exclude Slow optimizeRunOrder true } @Fast class MyFastSpec extends Specification { def "I’m fast as hell!"() { expect: true } @Slow def "sorry, can’t keep up..."() { expect: false } }

Slide 63

Slide 63 text

Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Jenkins, Bamboo, TeamCity Spock runs everywhere JUnit and Groovy run! Tooling

Slide 64

Slide 64 text

Eclipse, IDEA (specially IDEA) Tooling

Slide 65

Slide 65 text

Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Tooling

Slide 66

Slide 66 text

Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Jenkins, Bamboo, TeamCity Tooling

Slide 67

Slide 67 text

Eclipse, IDEA (specially IDEA) Ant, Maven, Gradle Jenkins, Bamboo, TeamCity Spock runs everywhere JUnit and Groovy run! Tooling

Slide 68

Slide 68 text

You write... Spock Under The Hood a = 1; b = 2; c = 4 expect: sum(a, b) == c

Slide 69

Slide 69 text

You write... Spock generates... Spock Under The Hood a = 1; b = 2; c = 4 expect: sum(a, b) == c def rec = new ValueRecorder() SpockRuntime.verifyCondition( rec.record(rec.record(sum(rec.record(a), rec.record(b)) == rec.record(c)))

Slide 70

Slide 70 text

You write... Spock generates... You see... Spock Under The Hood a = 1; b = 2; c = 4 expect: sum(a, b) == c def rec = new ValueRecorder() SpockRuntime.verifyCondition( rec.record(rec.record(sum(rec.record(a), rec.record(b)) == rec.record(c)))

Slide 71

Slide 71 text

Homepage http://spockframework.org Source Code http://github.spockframework.org/spock Documentation http://docs.spockframework.org/en/latest Spock Web Console http://webconsole.spockframework.org Spock Example Project http://github.spockframework.org/spock-example Slides and Code for this Presentation http://github.spockframework.org/smarter-testing-with-spock Mocks Aren’t Stubs http://martinfowler.com/articles/mocksArentStubs.html Q&A

Slide 72

Slide 72 text

No content