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