Why to write unit tests in Groovy (lightning talk), Q4/2011
-
Upload
jakub-holy -
Category
Education
-
view
483 -
download
1
description
Transcript of Why to write unit tests in Groovy (lightning talk), Q4/2011
- 1. for testing
2. What is Groovy? 3. Java is Groovy... public classMethodFakingFunctionMapperTest { privateMethodFakingFunctionMappermapper ; @Before public voidsetUp()throwsException { mapper=newMethodFakingFunctionMapper(); } @Test public voidshould_return_fakeMethod0_for_no_args_call(){ mapper .setCurrentExpression( "#{ns:myFun0()}" ); Method actualMethod =mapper .resolveFunction( "ns" ,"myFun0" ); assertSame ( FAKE_METHODS [0], actualMethod); } 4. (Yes, Groovy is a programming language for the JVM, based on Java) 5. ... but Groovy >> Java // Bean creation: deftestBean =n ewCustomer( surName:"Newt" , age:42 ) // Shortcut methods on File etc.: assert test. method () ==newFile( "expected.txt" ). getText () // Closure-based filter/find/transform on collections: assertcustomerFinder. findAll (). collect{it.surName}. sort ()== [ "Lizard" , "Newt" ] // Literals for maps and lists, comparison for equality via == : assertgetCapitalsMap() == [ "UK":"London" ,"CR":"Prague" ] // Literals for RegExps and utility methods on Matcher : assert( "dog1-dog2"=~ /dogd/). getAt ([ 0 , 1 ]) == [ "dog1" ,"dog2" ] 6. Why Groovy?
- Conscise syntax
- literals: [],[:],//,"""
7. can be dynamic # utility methods
- on Object, File, collections, Date, ...
8. with closures GSQL, [object/XML] builder, Gpath, ...
- Productivity
9. Productivity 10. Productivity 11. Productivity 12. Productivity 13. Productivity 14. Productivity 15. Productivity 16. We can even use Spock! @ S peck classDataDriven { def "maximum of two numbers" () { expect: Math.max(a, b) == c where: a