Slide 1

Slide 1 text

Smarter Testing With Spock P e t e r N i e d e r w i e s e r P r i n c i p a l E n g i n e e r , G r a d l e w a r e

Slide 2

Slide 2 text

What we’ll talk about Spock?! State Based Testing Data Driven Testing Interaction Based Testing Spock Extensions More Cool Stuff

Slide 3

Slide 3 text

Spock?!

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Spock Can... 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!

Slide 6

Slide 6 text

Getting Started Homepage http://spockframework.org Source Code https://github.com/spockframework/spock Spock Web Console http://meet.spockframework.org Spock Example Project http://downloads.spockframework.org https://github.com/spockframework/spock/tree/groovy-1.8/ spock-example Slides and Code for this Presentation https://github.com/spockframework/smarter-testing-with-spock

Slide 7

Slide 7 text

Who’s Using Spock? Geb Gradle GPars Spring? Apache Tapestry Griffon Grails Plugin Collective Spock Grails

Slide 8

Slide 8 text

Who’s Using Spock? (2) Energized Work SystemsForge IntelliGrape Software CTI Digital Software Projects eHarmony be2 Gennemtænkt IT Smarter Ecommerce BSkyB bemoko

Slide 9

Slide 9 text

State Based Testing

Slide 10

Slide 10 text

Classical Unit Testing Arrange Act Assert Given-When-Then State Based Testing

Slide 11

Slide 11 text

Classical Unit Testing Arrange Act Assert Given-When-Then State Based Testing Show me the code!

Slide 12

Slide 12 text

Recap: State Based Testing Blocks setup: cleanup: expect: given: when: then: where: and: Fixture Methods setup() cleanup() setupSpec() cleanupSpec() Instance and @Shared fields old() and thrown()

Slide 13

Slide 13 text

Data Driven Testing

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Interaction Based Testing

Slide 18

Slide 18 text

Interaction Based Testing Design and test how your objects communicate Mocking frameworks to the rescue Spock comes with its own mocking framework

Slide 19

Slide 19 text

Interaction Based Testing Design and test how your objects communicate Mocking frameworks to the rescue Spock comes with its own mocking framework Show me the code!

Slide 20

Slide 20 text

Recap: Interaction Based Testing Creating Mocking 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 21

Slide 21 text

Recap: Interaction Based Testing (2) Stubbing Mocking and Stubbing Impressing your friends // 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 22

Slide 22 text

Spock Extensions

Slide 23

Slide 23 text

Spock Extensions Listeners Interceptors Annotation-driven extensions Global extensions

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Built-in Extensions @Ignore @IgnoreRest @FailsWith @Timeout @AutoCleanup @Stepwise @RevertMetaClass @Rule Show me the code!

Slide 26

Slide 26 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 27

Slide 27 text

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

Slide 28

Slide 28 text

Grails Extension (2) class MyUnitSpec extends UnitSpec {! !! !def "domain mocking"() { !! !!! !setup:!! !! !!! !mockDomain(Person) ! !! !!! !when: !! !!! !new Person(name: name).save() ! !! !!! !then: !! !!! !Person.findByName(name) != null ! !! !!! !where: !! !!! !name = "bill" !!!!} }

Slide 29

Slide 29 text

Spring Extension class InjectionExamples extends Specification { !!!!@Autowired !!!!IService1 byType ! !!!!@Resource !!!!IService1 byName ! !!!!@Autowired !!!!ApplicationContext!context }

Slide 30

Slide 30 text

Other Cool Stuff

Slide 31

Slide 31 text

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 } } ~/.spock/SpockConfig.groovy, or on class path, or with -Dspock.configuration

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Q&A Homepage http://spockframework.org Source Code https://github.com/spockframework/spock Spock Web Console http://meet.spockframework.org Spock Example Project http://downloads.spockframework.org https://github.com/spockframework/spock/tree/groovy-1.8/ spock-example Slides and Code for this Presentation https://github.com/spockframework/smarter-testing-with-spock