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
Spock FrameworkParte 2
Ismael Soares
Revisão
IntroduçãoExemplos
Ciclo de vidaFasesBlocos
Tratamento de ExceçõesInterações
// 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
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
when:def x = Math.max(1, 2)
then:x == 2
Em resumo...expect:Math.max(1, 2) == 2
Bloco Expect
Bloco Expect
expect:Math.max(a, b) == c
where:a|b|c1|2|23|2|3
setup:def file = new File("/some/path")file.createNewFile()
// ...
cleanup:file.delete()
Bloco Cleanup
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
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
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
vs JUnit
Melhorando a visualização no console
@Unrolldef "name length"() { expect: name.size() == length
where: name << ["Kirk", "Spock", "Scotty"] length << [4, 5, 6]}
ReferênciaReferência
http://code.google.com/p/spock/
ObrigadoObrigado