3. // 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
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
8. def "computing the maximum of two numbers" () { expect : Math . max ( a , b ) == c where : a << [ 5 , 3 ] b << [ 1 , 9 ] c << [ 5 , 9 ] } Bloco Where
9. def "offered PC matches preferred configuration" () { when : def pc = shop . buyPc () then : pc . vendor == "Sunny" pc . clockRate >= 2333 pc . ram >= 4096 pc . os == "Linux" } Métodos Helper
10. def "offered PC matches preferred configuration" () { when : def pc = shop . buyPc () then : matchesPreferredConfiguration ( pc ) } void matchesPreferredConfiguration ( pc ) { assert pc . vendor == "Sunny" assert pc . clockRate >= 2333 assert pc . ram >= 4096 assert pc . os == "Linux" } Métodos Helper
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.