15
Spock Framework Parte 2 Ismael Soares

Spock Framework 2

  • Upload
    ismael

  • View
    1.971

  • Download
    4

Embed Size (px)

DESCRIPTION

Introdução ao framework dinâmico para criação de testes em aplicações Java e/ou Groovy

Citation preview

Page 1: Spock Framework 2

Spock FrameworkParte 2

Ismael Soares

Page 2: Spock Framework 2

Revisão

IntroduçãoExemplos

Ciclo de vidaFasesBlocos

Tratamento de ExceçõesInterações

Page 3: Spock Framework 2

// valor de retorno único, repetido indefinidamentesubscriber.isAlive() >> true                    

// valores de retorno múltiplos (qualquer coisa que o Groovy pode iterar). O último é repetido indefinidamente subscriber.isAlive() >>> [true, false, true]    

// valor de retorno personalizadodef random = new Random()subscriber.isAlive() >> { random.nextBoolean() }

// Ações personalizadas subscriber.isAlive() >> { throw new TimeoutException() }

subscriberDao.get(_) >> { args -> new Subscriber(args[0])}

Valores de retorno

Page 4: Spock Framework 2

when:publisher.publish.event

// sem ordem definida, subscriber1 pode ser notificado antes do subscriber2 then:1 * subscriber1.receive(event)1 * subscriber2.receive(event)

// subscriber3 deve ser notificado depois que subscriber1 e subscriber2 forem notificados and:1 * subscriber3.receive(event)

Validações Ordernadas

Page 5: Spock Framework 2

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

then:x == 2

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

Bloco Expect

Page 6: Spock Framework 2

Bloco Expect

expect:Math.max(a, b) == c

where:a|b|c1|2|23|2|3

Page 7: Spock Framework 2

setup:def file = new File("/some/path")file.createNewFile()

// ...

cleanup:file.delete()

Bloco Cleanup

Page 8: Spock Framework 2

def "computing the maximum of two numbers"() {  expect:  Math.max(a, b) == c

  where:  a << [5, 3]  b << [1, 9]  c << [5, 9]}

Bloco Where

Page 9: Spock Framework 2

def "offered PC matches preferred configuration"() {  when:  def pc = shop.buyPc()    then:  pc.vendor == "Sunny"  pc.clockRate >= 2333  pc.ram >= 4096  pc.os == "Linux"}

Métodos Helper

Page 10: Spock Framework 2

def "offered PC matches preferred configuration"() {  when:  def pc = shop.buyPc()    then:  matchesPreferredConfiguration(pc)}

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

Métodos Helper

Page 11: Spock Framework 2

vs JUnit

Page 12: Spock Framework 2

Melhorando a visualização no console

@Unrolldef "name length"() { expect: name.size() == length

where: name << ["Kirk", "Spock", "Scotty"] length << [4, 5, 6]}

Page 13: Spock Framework 2
Page 14: Spock Framework 2

ReferênciaReferência

http://code.google.com/p/spock/

Page 15: Spock Framework 2

ObrigadoObrigado