pocket talk; spock framework
TRANSCRIPT
![Page 1: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/1.jpg)
![Page 2: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/2.jpg)
Por que Spock?
![Page 3: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/3.jpg)
Spock para tudo
![Page 4: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/5.jpg)
Como eu...?
Resposta: “da mesma forma do JUnit”
![Page 6: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/6.jpg)
Specification
![Page 7: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/7.jpg)
Instance Fields
def obj = new ClassUnderSpecification()
def coll = new Collaborator()
![Page 8: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/8.jpg)
@Shared res = new VeryExpensiveResource()
static final PI = 3.141592654
Anotação @Shared
![Page 9: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/10.jpg)
Feature Methods
def "pushing an element on the stack"() {
// blocks go here
}
![Page 11: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/11.jpg)
Blocks
![Page 12: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/12.jpg)
Setup Blocks
setup:
def stack = new Stack()
def elem = "push me"
![Page 13: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/13.jpg)
When and Then Blocks
when: // stimulus
then: // response
![Page 14: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/14.jpg)
Expect Blocks
when: def x = Math.max(1, 2)then: x == 2
expect: Math.max(1, 2) == 2
![Page 15: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/17.jpg)
Erros de assert JUnit
![Page 18: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/18.jpg)
Erros de assert Spock
![Page 19: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/20.jpg)
Condições
when:stack.push(elem)
then:!stack.emptystack.size() == 1stack.peek() == elem
![Page 21: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/24.jpg)
Criando Mocks
def subscriber = Mock(Subscriber)
ou
Subscriber subscriber = Mock()
![Page 25: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/27.jpg)
Stubbing
interface Subscriber {
String receive(String message)
}
![Page 28: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/29.jpg)
Spies (evitar)
def subscriber = Spy(SubscriberImpl, constructorArgs: ["Fred"])
subscriber.receive(_) >> "ok"
![Page 30: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/30.jpg)
![Page 31: Pocket Talk; Spock framework](https://reader035.vdocuments.mx/reader035/viewer/2022062306/58f227b51a28abf27b8b45e7/html5/thumbnails/31.jpg)
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)