pocket talk; spock framework

31

Upload: infoway

Post on 21-Jan-2018

64 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Pocket Talk; Spock framework
Page 2: Pocket Talk; Spock framework

Por que Spock?

Page 3: Pocket Talk; Spock framework

Spock para tudo

Page 4: Pocket Talk; Spock framework

Spock usa o JUnit runner• Como incluir testes spock em meu projeto?

• Como rodar testes spock?

• Como debugar testes spock

• Como visualizar a cobertura de testes?

• como integrar testes spock com o sonar?

• como eu…?

Page 5: Pocket Talk; Spock framework

Como eu...?

Resposta: “da mesma forma do JUnit”

Page 6: Pocket Talk; Spock framework

Specification

Page 7: Pocket Talk; Spock framework

Instance Fields

def obj = new ClassUnderSpecification()

def coll = new Collaborator()

Page 8: Pocket Talk; Spock framework

@Shared res = new VeryExpensiveResource()

static final PI = 3.141592654

Anotação @Shared

Page 9: Pocket Talk; Spock framework

Fixture Methods

def setup() {} // run before every feature method

def cleanup() {} // run after every feature method

def setupSpec() {} // run before the first feature

method

def cleanupSpec() {} // run after the last feature

method

Page 10: Pocket Talk; Spock framework

Feature Methods

def "pushing an element on the stack"() {

// blocks go here

}

Page 11: Pocket Talk; Spock framework

Blocks

Page 12: Pocket Talk; Spock framework

Setup Blocks

setup:

def stack = new Stack()

def elem = "push me"

Page 13: Pocket Talk; Spock framework

When and Then Blocks

when: // stimulus

then: // response

Page 14: Pocket Talk; Spock framework

Expect Blocks

when: def x = Math.max(1, 2)then: x == 2

expect: Math.max(1, 2) == 2

Page 15: Pocket Talk; Spock framework

Cleanup Blockssetup:

def file = new File("/some/path")

file.createNewFile()

// ...

cleanup:

file.delete()

• Um bloco Cleanup só pode ser seguido por um bloco

where

• Normalmente usados para fechar uma conexão com

banco de dados ou outro recurso.

Page 16: Pocket Talk; Spock framework

Where Blocks

def "computing the maximum of two numbers"() {

expect:

Math.max(a, b) == c

where:

a << [5, 3]

b << [1, 9]

c << [5, 9]

}

Page 17: Pocket Talk; Spock framework

Erros de assert JUnit

Page 18: Pocket Talk; Spock framework

Erros de assert Spock

Page 19: Pocket Talk; Spock framework

Specifications as Documentation

setup: "open a database connection"

// code goes here

// code goes here

and: "seed the customer table"

// code goes here

and: "seed the product table"

// code goes here

Page 20: Pocket Talk; Spock framework

Condições

when:stack.push(elem)

then:!stack.emptystack.size() == 1stack.peek() == elem

Page 21: Pocket Talk; Spock framework

Condições de Exceção

when:

stack.pop()

then:

thrown(EmptyStackException)

stack.empty

when:

stack.pop()

then:

notThrown(NullPointerException)

Page 22: Pocket Talk; Spock framework

Helper Methodsdef "offered PC matches preferred configuration"() { when: def pc = shop.buyPc()

then: matchesPreferredConfiguration(pc)}

def matchesPreferredConfiguration(pc) { assert pc.vendor == "Sunny" assert pc.clockRate >= 2333 assert pc.ram >= 4096 assert pc.os == "Linux"}

Page 23: Pocket Talk; Spock framework

def "events are published to all subscribers"() {

def subscriber1 = Mock(Subscriber)

def subscriber2 = Mock(Subscriber)

def publisher = new Publisher()

publisher.add(subscriber1)

publisher.add(subscriber2)

when:

publisher.fire("event")

then:

1 * subscriber1.receive("event")

1 * subscriber2.receive("event")

}

Page 24: Pocket Talk; Spock framework

Criando Mocks

def subscriber = Mock(Subscriber)

ou

Subscriber subscriber = Mock()

Page 25: Pocket Talk; Spock framework

class PublisherSpec extends Specification {

Publisher publisher = new Publisher()

Subscriber subscriber = Mock()

Subscriber subscriber2 = Mock()

def setup() {

publisher.subscribers << subscriber // << is a Groovy

shorthand for List.add()

publisher.subscribers << subscriber2

}

}

Page 26: Pocket Talk; Spock framework

def "should send messages to all subscribers"() {

when:

publisher.send("hello")

then:

1 * subscriber.receive("hello")

1 * subscriber2.receive("hello")

}

Page 27: Pocket Talk; Spock framework

Stubbing

interface Subscriber {

String receive(String message)

}

Page 28: Pocket Talk; Spock framework

def subscriber = Mock(Subscriber)

subscriber.receive("message1") >> "ok"

subscriber.receive("message2") >> "fail"

subscriber.receive(_) >>> ["ok", "error", "error", "ok"]

subscriber.receive(_) >> { throw new InternalError("ouch") }

Page 29: Pocket Talk; Spock framework

Spies (evitar)

def subscriber = Spy(SubscriberImpl, constructorArgs: ["Fred"])

subscriber.receive(_) >> "ok"

Page 30: Pocket Talk; Spock framework
Page 31: Pocket Talk; Spock framework

Referências

http://codepipes.com/presentations/spock-vs-junit.pdfhttp://spockframework.github.io/spock/docs/1.0/interaction_based_testing.htmlhttp://spockframework.github.io/spock/docs/1.0/spock_primer.htmlTesting java with spock (Konstantinos Kapelonis)