Why to write unit tests in Groovy (lightning talk), Q4/2011

Embed Size (px)

DESCRIPTION

Why to use Groovy for unit testing instead of Java and how it increases productivity - its syntax being ~ 100% Java + ~ 1000% more, optional usage of static/dynamic typing, closures, hundreds of utility methods added to the standard JDK classes... . See http://theholyjava.wordpress.com/2011/10/18/only-a-masochist-would-write-unit-tests-in-java-be-smarter-use-groovy-or-jruby-or-st-else-similar/ for more

Citation preview

  • 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