Spock Framework Parte 2 Ismael Soares
Revisão Introdução Exemplos Ciclo de vida Fases Blocos Tratamento de Exceções Interações
// valor de retorno único, repetido indefinidamente subscriber . isAlive ()   >>   true                       // valores de retorno múltiplos (qualquer coisa que o Groovy pode iterar). O último é repetido indefinidamente  subscriber . isAlive ()   >>>   [ true ,   false ,   true ]       // valor de retorno personalizado def  random  =   new   Random () subscriber . isAlive ()   >>   {  random . nextBoolean ()   }   // Ações personalizadas  subscriber . isAlive ()   >>   {   throw   new   TimeoutException ()   }   subscriberDao.get (_)   >>   {   args ->   new Subscriber(args[0]) } Valores de retorno
when : publisher . publish . event // sem ordem definida, subscriber1 pode ser notificado antes do subscriber2  then : 1   *  subscriber1 . receive ( event ) 1   *  subscriber2 . receive ( event )   // subscriber3 deve ser notificado depois que subscriber1 e subscriber2 forem notificados  and : 1   *  subscriber3 . receive ( event )   Validações Ordernadas
when : def  x  =   Math . max ( 1 ,   2 ) then : x  ==   2 Em resumo... expect : Math . max ( 1 ,   2 )   ==   2 Bloco Expect
Bloco Expect expect : Math . max ( a ,   b )   ==  c where : a|b|c 1|2|2 3|2|3
setup : def  file  =   new   File ( "/some/path" ) file . createNewFile () // ... cleanup : file . delete () Bloco Cleanup
def   &quot;computing the maximum of two numbers&quot; ()   {   expect :    Math . max ( a ,  b )   ==  c    where :   a  <<   [ 5 ,   3 ]   b  <<   [ 1 ,   9 ]   c  <<   [ 5 ,   9 ] } Bloco Where
def   &quot;offered PC matches preferred configuration&quot; ()   {    when :    def  pc  =  shop . buyPc ()       then :   pc . vendor  ==   &quot;Sunny&quot;   pc . clockRate  >=   2333   pc . ram  >=   4096   pc . os  ==   &quot;Linux&quot; } Métodos Helper
def   &quot;offered PC matches preferred configuration&quot; ()   {    when :    def  pc  =  shop . buyPc ()       then :   matchesPreferredConfiguration ( pc ) } void  matchesPreferredConfiguration ( pc )   {    assert  pc . vendor  ==   &quot;Sunny&quot;    assert  pc . clockRate  >=   2333    assert  pc . ram  >=   4096    assert  pc . os  ==   &quot;Linux&quot; } Métodos Helper
vs JUnit
Melhorando a visualização no console @Unroll def   &quot;name length&quot; () { expect : name.size() == length where : name << [&quot;Kirk&quot;, &quot;Spock&quot;, &quot;Scotty&quot;] length << [4, 5, 6] }
 
Referência http://code.google.com/p/spock/
Obrigado

Spock Framework 2

  • 1.
    Spock Framework Parte2 Ismael Soares
  • 2.
    Revisão Introdução ExemplosCiclo de vida Fases Blocos Tratamento de Exceções Interações
  • 3.
    // valor deretorno único, repetido indefinidamente subscriber . isAlive () >> true                     // valores de retorno múltiplos (qualquer coisa que o Groovy pode iterar). O último é repetido indefinidamente subscriber . isAlive () >>> [ true , false , true ]     // valor de retorno personalizado def random = new Random () subscriber . isAlive () >> { random . nextBoolean () } // Ações personalizadas subscriber . isAlive () >> { throw new TimeoutException () } subscriberDao.get (_) >> { args -> new Subscriber(args[0]) } Valores de retorno
  • 4.
    when : publisher. publish . event // sem ordem definida, subscriber1 pode ser notificado antes do subscriber2 then : 1 * subscriber1 . receive ( event ) 1 * subscriber2 . receive ( event ) // subscriber3 deve ser notificado depois que subscriber1 e subscriber2 forem notificados and : 1 * subscriber3 . receive ( event ) Validações Ordernadas
  • 5.
    when : def x = Math . max ( 1 , 2 ) then : x == 2 Em resumo... expect : Math . max ( 1 , 2 ) == 2 Bloco Expect
  • 6.
    Bloco Expect expect: Math . max ( a , b ) == c where : a|b|c 1|2|2 3|2|3
  • 7.
    setup : def file = new File ( &quot;/some/path&quot; ) file . createNewFile () // ... cleanup : file . delete () Bloco Cleanup
  • 8.
    def &quot;computing the maximum of two numbers&quot; () {   expect :   Math . max ( a , b ) == c   where :   a << [ 5 , 3 ]   b << [ 1 , 9 ]   c << [ 5 , 9 ] } Bloco Where
  • 9.
    def &quot;offered PC matches preferred configuration&quot; () {   when :   def pc = shop . buyPc ()     then :   pc . vendor == &quot;Sunny&quot;   pc . clockRate >= 2333   pc . ram >= 4096   pc . os == &quot;Linux&quot; } Métodos Helper
  • 10.
    def &quot;offered PC matches preferred configuration&quot; () {   when :   def pc = shop . buyPc ()     then :   matchesPreferredConfiguration ( pc ) } void matchesPreferredConfiguration ( pc ) {   assert pc . vendor == &quot;Sunny&quot;   assert pc . clockRate >= 2333   assert pc . ram >= 4096   assert pc . os == &quot;Linux&quot; } Métodos Helper
  • 11.
  • 12.
    Melhorando a visualizaçãono console @Unroll def &quot;name length&quot; () { expect : name.size() == length where : name << [&quot;Kirk&quot;, &quot;Spock&quot;, &quot;Scotty&quot;] length << [4, 5, 6] }
  • 13.
  • 14.
  • 15.

Notas do Editor

  • #3 Altamente expressivo: escrevemos o código da mesma forma que fariamos para nos expressar. JUnit: compatível com diversas IDEs, ferramentas de build e testes de integração contínua.