+ All Categories
Home > Documents > Smarter Testing With Spock - 33rd...

Smarter Testing With Spock - 33rd...

Date post: 30-May-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
34
Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware
Transcript
Page 1: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Smarter Testing

With SpockP 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

Page 2: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

What we’ll talk about

Spock?!

State Based Testing

Data Driven Testing

Interaction Based Testing

Spock Extensions

More Cool Stuff

Page 3: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spock?!

Page 4: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spock is...

A developer testing framework...

for Groovy and Java applications...

based on Groovy...

fully compatible with JUnit...

but going beyond!

Page 5: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

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!

Page 6: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Getting Started

Homepagehttp://spockframework.org

Source Codehttps://github.com/spockframework/spock

Spock Web Consolehttp://meet.spockframework.org

Spock Example Projecthttp://downloads.spockframework.orghttps://github.com/spockframework/spock/tree/groovy-1.8/spock-example

Slides and Code for this Presentationhttps://github.com/spockframework/smarter-testing-with-spock

Page 7: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Who’s Using Spock?

Geb

Gradle GPars

Spring?

Apache Tapestry

Griffon

Grails Plugin Collective

Spock

Grails

Page 8: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Who’s Using Spock? (2)

Energized Work

SystemsForge

IntelliGrape Software

CTI Digital

Software Projects

eHarmonybe2

Gennemtænkt IT

Smarter Ecommerce

BSkyBbemoko

Page 9: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

State Based Testing

Page 10: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Classical Unit Testing

Arrange

Act

Assert

Given-When-Then

State Based Testing

Page 11: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Classical Unit Testing

Arrange

Act

Assert

Given-When-Then

State Based Testing

Show me th

e code!

Page 12: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Recap: State Based Testing

Blockssetup: cleanup: expect: given: when: then: where: and:

Fixture Methodssetup() cleanup() setupSpec() cleanupSpec()

Instance and @Shared fields

old() and thrown()

Page 13: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Data Driven Testing

Page 14: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Data Driven Testing

Test the same behavior...

with varying data!

Page 15: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Data Driven Testing

Test the same behavior...

with varying data!

Show me the code!

Page 16: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Recap: Data Driven Testing

where: block

Data tables

External data sources

@Unroll

Page 17: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Interaction Based Testing

Page 18: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Interaction Based Testing

Design and test how your objects communicate

Mocking frameworks to the rescue

Spock comes with its own mocking framework

Page 19: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Interaction Based Testing

Design and test how your objects communicate

Mocking frameworks to the rescue

Spock comes with its own mocking frameworkShow me the

code!

Page 20: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

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")

Page 21: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Recap: Interaction Based Testing (2)

Stubbing

Mocking and Stubbing

Impressing your friends

// now returns status codeString 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"]

(_.._) * _._(*_) >> _

Page 22: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spock Extensions

Page 23: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spock Extensions

Listeners

Interceptors

Annotation-driven extensions

Global extensions

Page 24: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Built-in Extensions@Ignore

@IgnoreRest

@FailsWith

@Timeout

@AutoCleanup

@Stepwise

@RevertMetaClass

@Rule

Page 25: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Built-in Extensions@Ignore

@IgnoreRest

@FailsWith

@Timeout

@AutoCleanup

@Stepwise

@RevertMetaClass

@Rule

Show me the code!

Page 26: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

External Extensions

spock-grails

spock-spring

spock-guice

spock-tapestry

spock-unitils

spock-griffon

spock-arquillian

spock-extensions http://github.com/robfletcher/spock-extensions

Page 27: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

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*’

Page 28: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

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"!!!!}}

Page 29: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spring Extension

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

Page 30: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Other Cool Stuff

Page 31: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Configuring Spock

runner {!! !filterStackTrace false!! !include Fast!! !exclude Slow!! !optimizeRunOrder true}

@Fastclass 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

Page 32: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Tooling

Eclipse, IDEA

Ant, Maven, Gradle

Jenkins, Bamboo, TeamCity

Spock runs everywhere JUnit and Groovy run!

Page 33: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Spock Under The Hood

You write...

Spock generates...

You see...

a = 1; b = 2; c = 4expect: 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

Page 34: Smarter Testing With Spock - 33rd Degree2012.33degree.org/pdf/LukeDaleySmarterTestingWithSpock.pdf · Smarter Testing With Spock Peter Niederwieser Principal Engineer,Gradleware.

Q&A

Homepagehttp://spockframework.org

Source Codehttps://github.com/spockframework/spock

Spock Web Consolehttp://meet.spockframework.org

Spock Example Projecthttp://downloads.spockframework.orghttps://github.com/spockframework/spock/tree/groovy-1.8/spock-example

Slides and Code for this Presentationhttps://github.com/spockframework/smarter-testing-with-spock


Recommended