tdd with spock @xpdays_ua

Post on 01-Dec-2014

1.227 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

TDD with

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

● 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

agenda● introduction

● what is ..?

● why Spock?

● details by examples

● summary

● q&a

What is TDD?

What is Spock?

Spock

Easy to learn

Expressive testing language

Good for unit and e2e tests

Compatible with JUnit

Extensible for specific needs

Informative

What about?..

Let’s start

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

Anatomy explained

● setup:

● expect:

● when:

● then:

● and:

Given When Then

Given When Then

Data driven

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

def testingInstance = new CreditCardValidator()

when: def res = testingInstance.isValid(cardNumber)

then: res == false

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

Data driven

Exceptional case

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

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

then: thrown(OrderEntryNotFoundException) }

Exceptional case

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 | _ '' | _}

Fixtures

TargetClass mock = Mock()

mock.find("itemId") >> item

1 * mock.receive("msg")

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

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

Mocking

Mocking

1 * mock.receive(!null)

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

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

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

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

Mocking

Extensions

@Ignore

@IgnoreRest

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

@FailsWith

@Timeout(10)

@AutoCleanup

@Stepwise

Extensions

Summary

pros● junit compliant

● expressive

● dynamic

● informative

cons● groovy knowledge

● groovy slowness*

➔ 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

Thank YOU!TDD with Spock

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

top related