realizando pruebas con spock
TRANSCRIPT
![Page 1: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/1.jpg)
REALIZANDO PRUEBAS CON SPOCK
ANDRES ALMIRAY @AALMIRAY ANDRESALMIRAY.COM
![Page 2: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/2.jpg)
@aalmiray
![Page 3: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/3.jpg)
@aalmiray
JCP Executive Committee Associate Seat
Committer
Committer
JSR377 Specification Lead
![Page 4: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/4.jpg)
@aalmiray
HTTP://SPOCKFRAMEWORK.ORG/
HTTPS://GITHUB.COM/SPOCKFRAMEWORK
![Page 5: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/5.jpg)
@aalmiray
AGENDA 1. Conceptos Básicos
2. Parametrización
3. Mock/Stub/Spy
4. Extensiones
![Page 6: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/6.jpg)
@aalmiray
CONCEPTOS BASICOS
![Page 7: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/7.jpg)
@aalmiray
CONCEPTOS BASICOS - JUNIT publicclassSampleTest{
@TestpublicvoidsimpleSpecificationExample(){
//given:
List<String>list=newArrayList<>();
//when:
list.add("Groovy");
//then:
assertThat(list,contains("Groovy"));
}
}
![Page 8: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/8.jpg)
@aalmiray
CONCEPTOS BASICOS - SPOCK publicclassSampleSpecextendsSpecification{
publicvoidsimpleSpecificationExample(){
given:
List<String>list=newArrayList<>();
when:
list.add("Groovy");
then:
assertlist.contains("Groovy");
assertlist.size()==1;
}
}
![Page 9: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/9.jpg)
@aalmiray
CONCEPTOS BASICOS - SPOCK classSampleSpecextendsSpecification{
def"AsimpleSpecificationExample"(){
given:
List<String>list=[]
when:
list<<'Groovy'
then:
list==['Groovy']
}
}
![Page 10: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/10.jpg)
@aalmiray
BLOQUES given / setup when then
expect
where
and
cleanup
![Page 11: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/11.jpg)
@aalmiray
FIXTURE METHODS defsetup(){…}
defcleanup(){…}
defsetupSpec(){…}
defcleanupSpec(){…}Spock
-----------------------------------------------------
@BeforepublicvoidsetUp(){…}JUnit
@AfterpublicvoidtearDown(){…}
@BeforeClasspublicstaticvoidsetUpTestCase(){…}
@AfterClasspublicstaticvoidtearDownTestCase(){…}
![Page 12: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/12.jpg)
@aalmiray
PARAMETRIZACION
![Page 13: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/13.jpg)
@aalmiray
PARAMETRIZACION @UnrollclassHelloServiceSpecificationextendsSpecification{def"InvokingsayHello('#input')yields'#output'"(){given:HelloServiceservice=newDefaultHelloService()when:Stringresult=service.sayHello(input)then:result==outputwhere:input<<['','Test']
output<<['Howdystranger!','HelloTest']}}
![Page 14: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/14.jpg)
@aalmiray
PARAMETRIZACION @UnrollclassHelloServiceSpecificationextendsSpecification{def"InvokingsayHello('#input')yields'#output'"(){given:HelloServiceservice=newDefaultHelloService()when:Stringresult=service.sayHello(input)then:result==outputwhere:input|output''|'Howdystranger!''Test'|'HelloTest'}}
![Page 15: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/15.jpg)
@aalmiray
MOCK/STUB/SPY
![Page 16: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/16.jpg)
@aalmiray
MOCK + INTERACCIONES classSampleSpecextendsSpecification{
def"Amockingexample"(){
given:
Collaboratorcollaborator=Mock()
1*collaborator.foo()>>'Spock'
Componentcomponent=newComponent(collaborator)
when:
Stringoutput=component.doit('isGroovy!')
then:
output=='SpockisGroovy!’
}
}
![Page 17: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/17.jpg)
@aalmiray
CARDINALIDAD 1*subscriber.receive("hello")
0*subscriber.receive("hello")
(1..3)*subscriber.receive("hello")
(1.._)*subscriber.receive("hello")
(_..3)*subscriber.receive("hello")
_*subscriber.receive("hello")
![Page 18: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/18.jpg)
@aalmiray
TARGET Y METODO 1*subscriber.receive("hello")
1*_.receive("hello")
1*subscriber._("hello")
1*subscriber./r.*e/("hello"))
1*_._("hello")
![Page 19: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/19.jpg)
@aalmiray
ARGUMENTOS 1*subscriber.receive("hello")
1*subscriber.receive(!"hello")
1*subscriber.receive()
1*subscriber.receive(_)
1*subscriber.receive(*_)
1*subscriber.receive(!null)
1*subscriber.receive(_asString)
1*subscriber.receive({it.size()>3})
![Page 20: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/20.jpg)
@aalmiray
ASCII ART? (_.._)*_._(*_)>>_
![Page 21: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/21.jpg)
@aalmiray
ASCII ART? (_.._)*_._(*_)>>_
12345
1. Cardinalidad0ainfinito2. CualquierMock/Stub/Spy3. Cualquiermétodo4. 0omásargumentos5. Valorderetornogenérico
![Page 22: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/22.jpg)
@aalmiray
EXTENSIONES
![Page 23: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/23.jpg)
@aalmiray
IGNORAR METODOS • @Ignore
• Ignora un solo método
• @IgnoreRest
• Ignora los demás métodos
• @IgnoreIf
• Ignora un método de manera condicional
![Page 24: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/24.jpg)
@aalmiray
@IGNOREIF classSampleSpecextendsSpecification{
@IgnoreIf({System.getBoolean("fast")})
def"Aexpensivedatadriventest"(){
given:
...
}
}
![Page 25: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/25.jpg)
@aalmiray
@STEPWISE @Stepwise
classBakeACakeSpecextendsSpecification{
def"Fetchingredients"(){...}
def"Mixingredients"(){...}
def"Decoratebake"(){...}
def"Bakeit!"(){...}
def"Serveandenjoy"(){...}
}
![Page 26: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/26.jpg)
@aalmiray
HTTP://ANDRESALMIRAY.COM/NEWSLETTER
HTTP://ANDRESALMIRAY.COM/EDITORIAL
![Page 27: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/27.jpg)
@aalmiray
![Page 28: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/28.jpg)
@aalmiray
http://www.jespanol.org
![Page 29: Realizando Pruebas con Spock](https://reader033.vdocuments.net/reader033/viewer/2022051123/5a6481c17f8b9a57568b49bb/html5/thumbnails/29.jpg)
GRACIAS!
ANDRES ALMIRAY @AALMIRAY ANDRESALMIRAY.COM