Upload
infoway
View
61
Download
0
Embed Size (px)
Citation preview
Por que Spock?
Spock para tudo
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…?
Como eu...?
Resposta: “da mesma forma do JUnit”
Specification
Instance Fields
def obj = new ClassUnderSpecification()
def coll = new Collaborator()
@Shared res = new VeryExpensiveResource()
static final PI = 3.141592654
Anotação @Shared
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
Feature Methods
def "pushing an element on the stack"() {
// blocks go here
}
Blocks
Setup Blocks
setup:
def stack = new Stack()
def elem = "push me"
When and Then Blocks
when: // stimulus
then: // response
Expect Blocks
when: def x = Math.max(1, 2)then: x == 2
expect: Math.max(1, 2) == 2
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.
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]
}
Erros de assert JUnit
Erros de assert Spock
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
Condições
when:stack.push(elem)
then:!stack.emptystack.size() == 1stack.peek() == elem
Condições de Exceção
when:
stack.pop()
then:
thrown(EmptyStackException)
stack.empty
when:
stack.pop()
then:
notThrown(NullPointerException)
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"}
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")
}
Criando Mocks
def subscriber = Mock(Subscriber)
ou
Subscriber subscriber = Mock()
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
}
}
def "should send messages to all subscribers"() {
when:
publisher.send("hello")
then:
1 * subscriber.receive("hello")
1 * subscriber2.receive("hello")
}
Stubbing
interface Subscriber {
String receive(String message)
}
def subscriber = Mock(Subscriber)
subscriber.receive("message1") >> "ok"
subscriber.receive("message2") >> "fail"
subscriber.receive(_) >>> ["ok", "error", "error", "ok"]
subscriber.receive(_) >> { throw new InternalError("ouch") }
Spies (evitar)
def subscriber = Spy(SubscriberImpl, constructorArgs: ["Fred"])
subscriber.receive(_) >> "ok"
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)