realizando pruebas con spock

29
REALIZANDO PRUEBAS CON SPOCK ANDRES ALMIRAY @AALMIRAY ANDRESALMIRAY.COM

Upload: andres-almiray

Post on 21-Jan-2018

187 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Realizando Pruebas con Spock

REALIZANDO PRUEBAS CON SPOCK

ANDRES ALMIRAY @AALMIRAY ANDRESALMIRAY.COM

Page 2: Realizando Pruebas con Spock

@aalmiray

Page 3: Realizando Pruebas con Spock

@aalmiray

JCP Executive Committee Associate Seat

Committer

Committer

JSR377 Specification Lead

Page 4: Realizando Pruebas con Spock

@aalmiray

HTTP://SPOCKFRAMEWORK.ORG/

HTTPS://GITHUB.COM/SPOCKFRAMEWORK

Page 5: Realizando Pruebas con Spock

@aalmiray

AGENDA 1.  Conceptos Básicos

2.  Parametrización

3.  Mock/Stub/Spy

4.  Extensiones

Page 6: Realizando Pruebas con Spock

@aalmiray

CONCEPTOS BASICOS

Page 7: Realizando Pruebas con Spock

@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

@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

@aalmiray

CONCEPTOS BASICOS - SPOCK classSampleSpecextendsSpecification{

def"AsimpleSpecificationExample"(){

given:

List<String>list=[]

when:

list<<'Groovy'

then:

list==['Groovy']

}

}

Page 10: Realizando Pruebas con Spock

@aalmiray

BLOQUES given / setup when then

expect

where

and

cleanup

Page 11: Realizando Pruebas con Spock

@aalmiray

FIXTURE METHODS defsetup(){…}

defcleanup(){…}

defsetupSpec(){…}

defcleanupSpec(){…}Spock

-----------------------------------------------------

@BeforepublicvoidsetUp(){…}JUnit

@AfterpublicvoidtearDown(){…}

@BeforeClasspublicstaticvoidsetUpTestCase(){…}

@AfterClasspublicstaticvoidtearDownTestCase(){…}

Page 12: Realizando Pruebas con Spock

@aalmiray

PARAMETRIZACION

Page 13: Realizando Pruebas con Spock

@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

@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

@aalmiray

MOCK/STUB/SPY

Page 16: Realizando Pruebas con Spock

@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

@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

@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

@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

@aalmiray

ASCII ART? (_.._)*_._(*_)>>_

Page 21: Realizando Pruebas con Spock

@aalmiray

ASCII ART? (_.._)*_._(*_)>>_

12345

1.  Cardinalidad0ainfinito2.  CualquierMock/Stub/Spy3.  Cualquiermétodo4.  0omásargumentos5.  Valorderetornogenérico

Page 22: Realizando Pruebas con Spock

@aalmiray

EXTENSIONES

Page 23: Realizando Pruebas con Spock

@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

@aalmiray

@IGNOREIF classSampleSpecextendsSpecification{

@IgnoreIf({System.getBoolean("fast")})

def"Aexpensivedatadriventest"(){

given:

...

}

}

Page 25: Realizando Pruebas con Spock

@aalmiray

@STEPWISE @Stepwise

classBakeACakeSpecextendsSpecification{

def"Fetchingredients"(){...}

def"Mixingredients"(){...}

def"Decoratebake"(){...}

def"Bakeit!"(){...}

def"Serveandenjoy"(){...}

}

Page 26: Realizando Pruebas con Spock

@aalmiray

HTTP://ANDRESALMIRAY.COM/NEWSLETTER

HTTP://ANDRESALMIRAY.COM/EDITORIAL

Page 27: Realizando Pruebas con Spock

@aalmiray

Page 28: Realizando Pruebas con Spock

@aalmiray

http://www.jespanol.org

Page 29: Realizando Pruebas con Spock

GRACIAS!

ANDRES ALMIRAY @AALMIRAY ANDRESALMIRAY.COM