tdd with spock @xpdays_ua

35
TDD with Spock Izzet Mustafayev@EPAM Systems @webdizz http://webdizz.name

Upload: izzet-mustafaiev

Post on 01-Dec-2014

1.227 views

Category:

Technology


0 download

DESCRIPTION

Slides from my talk @ XPDays Ukarine 2013 regarding TDD with Spock Framework at Java project

TRANSCRIPT

Page 1: TDD with Spock @xpdays_ua

TDD with

SpockIzzet Mustafayev@EPAM Systems@webdizz http://webdizz.name

Page 2: TDD with Spock @xpdays_ua

● SA at EPAM Systems

● primary skill is Java

● hands-on-coding with Ruby, Groovy and little bit

of Scala

● passion about agile, clean code practices and

devops

Page 3: TDD with Spock @xpdays_ua

agenda● introduction

● what is ..?

● why Spock?

● details by examples

● summary

● q&a

Page 4: TDD with Spock @xpdays_ua

What is TDD?

Page 5: TDD with Spock @xpdays_ua

What is Spock?

Page 6: TDD with Spock @xpdays_ua

Spock

Page 7: TDD with Spock @xpdays_ua

Easy to learn

Page 8: TDD with Spock @xpdays_ua

Expressive testing language

Page 9: TDD with Spock @xpdays_ua

Good for unit and e2e tests

Page 10: TDD with Spock @xpdays_ua

Compatible with JUnit

Page 11: TDD with Spock @xpdays_ua

Extensible for specific needs

Page 12: TDD with Spock @xpdays_ua

Informative

Page 13: TDD with Spock @xpdays_ua

What about?..

Page 14: TDD with Spock @xpdays_ua

Let’s start

Page 15: TDD with Spock @xpdays_ua

Setup

// Gradle exampleapply plugin: "groovy"//....dependencies { groovy "org.codehaus.groovy:groovy-all:2.1.5" testCompile "org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT"}

# Buildr example (ruby)require 'buildr/groovy'

# ...

define "tdd-with-spock" do

test.compile.with 'org.spockframework:spock-core:

jar:0.7-groovy-2.0'

end

Page 16: TDD with Spock @xpdays_ua

Anatomy explained

Page 17: TDD with Spock @xpdays_ua

● setup:

● expect:

● when:

● then:

● and:

Given When Then

Page 18: TDD with Spock @xpdays_ua

Given When Then

Page 19: TDD with Spock @xpdays_ua

Data driven

@Unrolldef 'should not allow value: #cardNumber'(){

def testingInstance = new CreditCardValidator()

when: def res = testingInstance.isValid(cardNumber)

then: res == false

where: cardNumber | _ null | _ '' | _}

Page 20: TDD with Spock @xpdays_ua

Data driven

Page 21: TDD with Spock @xpdays_ua

Exceptional case

def 'it should not get not existing order entry '() {

when: skuCode = "anotherSkuCode" OrderUtils.getOrderEntryBySku(orderModel, skuCode)

then: thrown(OrderEntryNotFoundException) }

Page 22: TDD with Spock @xpdays_ua

Exceptional case

Page 23: TDD with Spock @xpdays_ua

Fixtures

def testingInstance

def setup(){testingInstance = new CreditCardValidator()

}

@Unrolldef 'should not allow value: #cardNumber'(){ when: def res = testingInstance.isValid(cardNumber) then: res == false where: cardNumber | _ null | _ '' | _}

Page 24: TDD with Spock @xpdays_ua

Fixtures

Page 25: TDD with Spock @xpdays_ua

TargetClass mock = Mock()

mock.find("itemId") >> item

1 * mock.receive("msg")

(1..3) * mock.receive(_)

(1.._) * mock.receive(_ as String)

Mocking

Page 26: TDD with Spock @xpdays_ua

Mocking

1 * mock.receive(!null)

1 * mock.receive({it.contains("m")})

1 * mock.find({it.contains("m")) >> item

(1.._) * mock./(set[A-Za-z]*)/(_)

(_.._) * _._(*_) >> _ // :)

Page 27: TDD with Spock @xpdays_ua

Mocking

Page 28: TDD with Spock @xpdays_ua

Extensions

@Ignore

@IgnoreRest

@IgnoreIf({System.getProperty('app.env’).contains('ci’)})

@FailsWith

@Timeout(10)

@AutoCleanup

@Stepwise

Page 29: TDD with Spock @xpdays_ua

Extensions

Page 30: TDD with Spock @xpdays_ua

Summary

Page 31: TDD with Spock @xpdays_ua

pros● junit compliant

● expressive

● dynamic

● informative

Page 32: TDD with Spock @xpdays_ua

cons● groovy knowledge

● groovy slowness*

Page 33: TDD with Spock @xpdays_ua

➔ 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

➔ Talk’s source code http://github.com/webdizz/tdd-

with-spock

Reference

Page 34: TDD with Spock @xpdays_ua
Page 35: TDD with Spock @xpdays_ua

Thank YOU!TDD with Spock

Izzet Mustafayev@EPAM Systems@webdizz http://webdizz.name