spock: boldly go where no test has gone before - devoxx12

14

Upload: andres-almiray

Post on 01-Dec-2014

1.247 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Spock: boldly go where no test has gone before - Devoxx12
Page 2: Spock: boldly go where no test has gone before - Devoxx12

Spock:���Boldly go where no test has

gone before

Andres Almiray Canoo Fellow

Canoo Engineering AG

@aalmiray

Page 3: Spock: boldly go where no test has gone before - Devoxx12

3

Andres Almiray

Speaker Bio

■  Java developer since the beginning

■  True believer in open source

■  Groovy committer since 2007

■  Project lead of the Griffon framework

■  Currently working for

Page 4: Spock: boldly go where no test has gone before - Devoxx12

What is Spock?

Where, Who, How?

■ http://spockframework.org

■ Peter Niederwiser @pniederw

■ Groovy based Testing Language

■ Byte code manipulation at compile time

■  Inspired by JUnit, Rspec, jMock, Mockito, Groovy, Scala and Vulcans

4

Why?

■  Expressive testing language

■  Easy to learn

■  Usable from unit to end-to-end level

■  Leverages Groovy language features

■  Runs with JUnit: compatible with IDEs, build tools & CI

■  Extensible for specialized testing scenarios

Page 5: Spock: boldly go where no test has gone before - Devoxx12

5

First Cut class HelloSpock extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 } }

Page 6: Spock: boldly go where no test has gone before - Devoxx12

6

Introducing a bug class HelloSpock extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 7 } }

Page 7: Spock: boldly go where no test has gone before - Devoxx12

7

A wild error appears! Condition not satisfied:

name.size() == length

| | | |

| 6 | 7

Scotty false

at com.acme.HelloSpock.length of Spock's and his friends' names(HelloSpock.groovy:6)

Page 8: Spock: boldly go where no test has gone before - Devoxx12

Feature List (1)

Blocks

■ given: preconditions, data fixtures, etc.

■ when: actions that trigger some outcome

■ then: makes assertions about outcome

■ expect: short alternative to when & then

■ where: applies varied inputs

■ and: sub-divides other blocks

■ setup: alias for given

■ cleanup: post-conditions, housekeeping, etc.

8

Page 9: Spock: boldly go where no test has gone before - Devoxx12

Feature List (2)

Lifecycle

■ setup

■ cleanup

■ setupSpec

■ cleanupSpec

9

Data Driven

■  List based variables

■  Table based variables

■  @Unroll

■  @Shared

Page 10: Spock: boldly go where no test has gone before - Devoxx12

10

Unrolling class HelloSpock extends spock.lang.Specification {

@Unroll

def "Length of #name should be #name.size(), actual value is #length"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 } }

Page 11: Spock: boldly go where no test has gone before - Devoxx12

Unrolling as seen by an IDE

11

Page 12: Spock: boldly go where no test has gone before - Devoxx12

12

Interactions (Mocks) class BindableSpec extends Specification {

def "Model properties are observable"() {

given:

def model = new Model()

def listener = Mock(PropertyChangeListener)

when:

model.addPropertyChangeListener(listener)

model.name = 'Groovy'

model.name = 'Java'

then:

1 * listener.propertyChange({it.newValue == 'Groovy'})

1 * listener.propertyChange({it.newValue == 'Java'})

}

}

Page 13: Spock: boldly go where no test has gone before - Devoxx12

But wait, there’s more!

■ http://docs.spockframework.org/en/latest/

■ Spock is extensible via plugins

■ Functional web testing with GEB

■ Plugins exist for Grails and Griffon

■ Next release will be 1.0

13

Page 14: Spock: boldly go where no test has gone before - Devoxx12

Q&A Andres Almiray

@aalmiray