0
Boosting your testing productivity with  G r oo v y <ul><ul><li>Andres Almiray </li></ul></ul>
Have fun while programming tests <ul><ul><li>G o a l </li></ul></ul>
Agenda <ul><li>What is  G r oo v y </li></ul><ul><li>G r oo v y  + Testing Frameworks </li></ul><ul><li>Mocking with  G r ...
W hat is  G r oo v y ?
HelloWorld.java <ul><li>public class  HelloWorld { </li></ul><ul><li>String name; </li></ul><ul><li>public   void  setName...
HelloWorld.groovy <ul><li>public class  HelloWorld { </li></ul><ul><li>String name; </li></ul><ul><li>public   void  setNa...
Equivalent HelloWorld 100%  G r oo v y <ul><li>class  HelloWorld { </li></ul><ul><li>String name </li></ul><ul><li>def  gr...
1 st  Mantra <ul><li>Java is  G r oo v y ,  G r oo v y  is Java </li></ul><ul><li>Every single Java class is a  G r oo v y...
Common Gotchas <ul><li>Java Array initializers are not supported, but lists can be coerced into arrays. </li></ul><ul><li>...
2 nd  Mantra <ul><li>Groovy is Java and  G r oo v y  is not Java </li></ul><ul><li>Flat learning curve for Java developers...
G r oo v y  + Testing Frameworks <ul><li>Any  G r oo v y  script may become a testcase </li></ul><ul><ul><li>assert keywor...
Testing exceptions in Java <ul><li>public class JavaExceptionTestCase  extends TestCase  { </li></ul><ul><li>public void t...
Testing exceptions in Groovy <ul><li>class   GroovyExceptionTestCase  extends GroovyTestCase  { </li></ul><ul><li>void tes...
But how do I run  G r oo v y  tests? <ul><li>Pick your favourite IDE!  </li></ul><ul><ul><li>IDEA </li></ul></ul><ul><ul><...
Mocking with  G r oo v y <ul><li>Known mocking libraries </li></ul><ul><ul><li>EasyMock – record/replay </li></ul></ul><ul...
Dynamic Proxies <ul><li>This feature allows you to define a java.lang.reflect.Proxy using either a Closure or Map +  G r o...
Dynamic Proxies with Proxy-o-Matic <ul><li>Proxy-o-Matic extends  G r oo v y 's built-in proxy support by allowing the fol...
Dynamic Proxies <ul><ul><li>Demo </li></ul></ul>
StubFor/MockFor <ul><li>caller – collaborator </li></ul><ul><li>mocks/stubs define expectations on collaborators </li></ul...
G r oo v y  Mocks <ul><ul><li>Demo </li></ul></ul>
Testing Databases <ul><li>DbUnit: a Junit extension for testing databases </li></ul><ul><li>Several options at your dispos...
Inline XML dataset <ul><li>import org.dbunit.* </li></ul><ul><li>import org.junit.* </li></ul><ul><li>class MyDBTestCase {...
Compile-checked dataset <ul><li>import org.dbunit.* </li></ul><ul><li>import org.junit.* </li></ul><ul><li>Import groovy.x...
Building Test Data <ul><li>Builders are a great option for creating hierarchical data </li></ul><ul><li>MarkupBuilder -> H...
ObjectGraphBuilder <ul><ul><li>Demo </li></ul></ul>
Functional UI Testing <ul><li>These tests usually require more setup </li></ul><ul><li>Non-developers usually like to driv...
G r oo v y  to the rescue! <ul><li>[Any of the following] +  G r oo v y  = success! </li></ul><ul><li>Web testing -> Canoo...
FEST + Easyb <ul><ul><li>Demo </li></ul></ul>
For More Information <ul><li>h tt p : // groovy.codehaus.org </li></ul><ul><li>h tt p : // junit.org </li></ul><ul><li>h t...
Q   &   A
Thank You!
Upcoming SlideShare
Loading in...5
×

Svcc Groovy Testing

1,865

Published on

Boosting your Testing Productivity with Groovy

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,865
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Svcc Groovy Testing"

  1. 1. Boosting your testing productivity with G r oo v y <ul><ul><li>Andres Almiray </li></ul></ul>
  2. 2. Have fun while programming tests <ul><ul><li>G o a l </li></ul></ul>
  3. 3. Agenda <ul><li>What is G r oo v y </li></ul><ul><li>G r oo v y + Testing Frameworks </li></ul><ul><li>Mocking with G r oo v y </li></ul><ul><li>Testing Databases </li></ul><ul><li>Building Test Data </li></ul><ul><li>Functional UI Testing </li></ul><ul><li>Resources </li></ul>
  4. 4. W hat is G r oo v y ?
  5. 5. HelloWorld.java <ul><li>public class HelloWorld { </li></ul><ul><li>String name; </li></ul><ul><li>public void setName(String name)‏ </li></ul><ul><li>{ this .name = name; } </li></ul><ul><li>public String getName(){ return name; } </li></ul><ul><li>public String greet()‏ </li></ul><ul><li>{ return “Hello “ + name; } </li></ul><ul><li>public static void main(String args[]){ </li></ul><ul><li>HelloWorld helloWorld = new HelloWorld(); </li></ul><ul><li>helloWorld.setName( “Groovy” ); </li></ul><ul><li>System.err.println( helloWorld.greet() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  6. 6. HelloWorld.groovy <ul><li>public class HelloWorld { </li></ul><ul><li>String name; </li></ul><ul><li>public void setName(String name)‏ </li></ul><ul><li>{ this .name = name; } </li></ul><ul><li>public String getName(){ return name; } </li></ul><ul><li>public String greet()‏ </li></ul><ul><li>{ return “Hello “ + name; } </li></ul><ul><li>public static void main(String args[]){ </li></ul><ul><li>HelloWorld helloWorld = new HelloWorld(); </li></ul><ul><li>helloWorld.setName( “Groovy” ); </li></ul><ul><li>System.err.println( helloWorld.greet() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Equivalent HelloWorld 100% G r oo v y <ul><li>class HelloWorld { </li></ul><ul><li>String name </li></ul><ul><li>def greet() { &quot;Hello $name&quot; } </li></ul><ul><li>} </li></ul><ul><li>def helloWorld = new HelloWorld(name : &quot; Groovy&quot; )‏ </li></ul><ul><li>println helloWorld.greet()‏ </li></ul>
  8. 8. 1 st Mantra <ul><li>Java is G r oo v y , G r oo v y is Java </li></ul><ul><li>Every single Java class is a G r oo v y class, the inverse is also true. This means your Java can call my G r oo v y in vice versa, without any clutter nor artificial bridge. </li></ul><ul><li>G r oo v y has the same memory and security models as Java. </li></ul><ul><li>Almost 98% Java code is G r oo v y code, meaning you can in most cases rename *.java to *.groovy and it will work. </li></ul>
  9. 9. Common Gotchas <ul><li>Java Array initializers are not supported, but lists can be coerced into arrays. </li></ul><ul><li>Inner class definitions are not supported (yet!!). </li></ul>
  10. 10. 2 nd Mantra <ul><li>Groovy is Java and G r oo v y is not Java </li></ul><ul><li>Flat learning curve for Java developers, start with straight Java syntax then move on to a groovier syntax as you feel comfortable. </li></ul><ul><li>G r oo v y delivers closures, meta-programming, new operators, operator overloading, enhanced POJOs, properties, native syntax for Maps and Lists, regular expressions, enhanced class casting, optional typing, and more! </li></ul>
  11. 11. G r oo v y + Testing Frameworks <ul><li>Any G r oo v y script may become a testcase </li></ul><ul><ul><li>assert keyword enabled by default </li></ul></ul><ul><li>G r oo v y provides a GroovyTestCase base class </li></ul><ul><ul><li>Easier to test exception throwing code </li></ul></ul><ul><li>Junit 4.x and TestNG ready, G r oo v y supports JDK5+ features </li></ul><ul><ul><li>Annotations </li></ul></ul><ul><ul><li>Static imports </li></ul></ul><ul><ul><li>Enums </li></ul></ul>
  12. 12. Testing exceptions in Java <ul><li>public class JavaExceptionTestCase extends TestCase { </li></ul><ul><li>public void testExceptionThrowingCode() { </li></ul><ul><li>try { </li></ul><ul><li>new MyService().doSomething(); </li></ul><ul><li>fail(&quot;MyService.doSomething has been implemented&quot;); </li></ul><ul><li>}catch( UnsupportedOperationException expected ){ </li></ul><ul><li>// everything is ok if we reach this block </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Testing exceptions in Groovy <ul><li>class GroovyExceptionTestCase extends GroovyTestCase { </li></ul><ul><li>void testExceptionThrowingCode() { </li></ul><ul><li>shouldFail( UnsupportedOperationException ){ </li></ul><ul><li>new MyService().doSomething()‏ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  14. 14. But how do I run G r oo v y tests? <ul><li>Pick your favourite IDE! </li></ul><ul><ul><li>IDEA </li></ul></ul><ul><ul><li>Eclipse </li></ul></ul><ul><ul><li>NetBeans </li></ul></ul><ul><li>Command line tools </li></ul><ul><ul><li>Ant </li></ul></ul><ul><ul><li>Gant </li></ul></ul><ul><ul><li>Maven </li></ul></ul><ul><ul><li>Good ol’ G r oo v y shell/console </li></ul></ul>
  15. 15. Mocking with G r oo v y <ul><li>Known mocking libraries </li></ul><ul><ul><li>EasyMock – record/replay </li></ul></ul><ul><ul><li>JMock – write expectations as you go </li></ul></ul><ul><li>Use dynamic proxies as stubs </li></ul><ul><ul><li>built-in proxy support </li></ul></ul><ul><ul><li>extended proxies with Proxy-o-Matic </li></ul></ul><ul><li>Use StubFor/MockFor </li></ul><ul><ul><li>inspired by EasyMock </li></ul></ul><ul><ul><li>no external libraries required (other than G r oo v y )‏ </li></ul></ul>
  16. 16. Dynamic Proxies <ul><li>This feature allows you to define a java.lang.reflect.Proxy using either a Closure or Map + G r oo v y casting </li></ul><ul><li>You can proxy interfaces, abstract and concrete classes this way </li></ul><ul><li>The syntax for proxying a Map looks like JSON </li></ul><ul><li>No need to supply implementations for all methods, just proxy the ones you need </li></ul><ul><li>CAVEAT: map proxying does not allow overloaded methods </li></ul>
  17. 17. Dynamic Proxies with Proxy-o-Matic <ul><li>Proxy-o-Matic extends G r oo v y 's built-in proxy support by allowing the following </li></ul><ul><ul><li>define overloaded methods </li></ul></ul><ul><ul><li>call its own methods from within </li></ul></ul><ul><ul><li>proxy more than 1 interface at a time </li></ul></ul><ul><ul><li>proxy from Expandos as well </li></ul></ul><ul><li>CAVEAT: it can only proxy interfaces for the time being </li></ul><ul><li>http://groovy.codehaus.org/Proxy-o-Matic </li></ul>
  18. 18. Dynamic Proxies <ul><ul><li>Demo </li></ul></ul>
  19. 19. StubFor/MockFor <ul><li>caller – collaborator </li></ul><ul><li>mocks/stubs define expectations on collaborators </li></ul><ul><li>mocks are strict, expectation must be fulfilled both in order of invocation and cardinality. </li></ul><ul><li>stubs are loose, expectations must fulfil cardinality but may be invoked in any order. </li></ul><ul><li>CAVEAT: can be used to mock both G r oo v y and Java collaborators, caller must be G r oo v y though. </li></ul>
  20. 20. G r oo v y Mocks <ul><ul><li>Demo </li></ul></ul>
  21. 21. Testing Databases <ul><li>DbUnit: a Junit extension for testing databases </li></ul><ul><li>Several options at your disposal </li></ul><ul><ul><li>Old school – extend DatabaseTestCase </li></ul></ul><ul><ul><li>Flexible – use an IDataBaseTester implementation </li></ul></ul><ul><ul><li>Roll your own Database testcase </li></ul></ul>
  22. 22. Inline XML dataset <ul><li>import org.dbunit.* </li></ul><ul><li>import org.junit.* </li></ul><ul><li>class MyDBTestCase { </li></ul><ul><li>IDatabaseTester db </li></ul><ul><li>@BeforeClass void init(){ </li></ul><ul><li>db = new JdbcDatabaseTester(&quot;org.hsqldb.jdbcDriver&quot;, </li></ul><ul><li>&quot;jdbc:hsqldb:sample&quot;, &quot;sa&quot;, &quot;&quot; )‏ </li></ul><ul><li>// insert table schema </li></ul><ul><li>def dataset = &quot;&quot;&quot; </li></ul><ul><li><dataset> </li></ul><ul><li><company name=&quot;Acme&quot;/> </li></ul><ul><li><employee name=&quot;Duke&quot; company_id=&quot;1&quot;> </li></ul><ul><li></dataset> </li></ul><ul><li>&quot;&quot;&quot; </li></ul><ul><li>db.dataset = new FlatXmlDataSet( new StringReader(dataset) )‏ </li></ul><ul><li>db.onSetUp()‏ </li></ul><ul><li>} </li></ul><ul><li>@AfterClass void exit() { db.onTearDown() } </li></ul><ul><li>} </li></ul>
  23. 23. Compile-checked dataset <ul><li>import org.dbunit.* </li></ul><ul><li>import org.junit.* </li></ul><ul><li>Import groovy.xml.MarkupBuilder </li></ul><ul><li>class MyDBTestCase { </li></ul><ul><li>IDatabaseTester db </li></ul><ul><li>@BeforeClass void init(){ </li></ul><ul><li>db = new JdbcDatabaseTester(&quot;org.hsqldb.jdbcDriver&quot;, </li></ul><ul><li>&quot;jdbc:hsqldb:sample&quot;, &quot;sa&quot;, &quot;&quot; )‏ </li></ul><ul><li>// insert table schema </li></ul><ul><li>def dataset = new MarkupBuilder().dataset { </li></ul><ul><li>company( name: Acme )‏ </li></ul><ul><li>employee( name: &quot;Duke&quot;, company_id: 1 )‏ </li></ul><ul><li>} </li></ul><ul><li>db.dataset = new FlatXmlDataSet( new StringReader(dataset) )‏ </li></ul><ul><li>db.onSetUp()‏ </li></ul><ul><li>} </li></ul><ul><li>@AfterClass void exit() { db.onTearDown() } </li></ul><ul><li>} </li></ul>
  24. 24. Building Test Data <ul><li>Builders are a great option for creating hierarchical data </li></ul><ul><li>MarkupBuilder -> HTML/XML </li></ul><ul><li>ObjectGraphBuilder -> bean graph </li></ul>
  25. 25. ObjectGraphBuilder <ul><ul><li>Demo </li></ul></ul>
  26. 26. Functional UI Testing <ul><li>These tests usually require more setup </li></ul><ul><li>Non-developers usually like to drive these tests </li></ul><ul><li>Developers usually don’t like to code these tests </li></ul><ul><li>No Functional Testing => unhappy customer => unhappy developer </li></ul>
  27. 27. G r oo v y to the rescue! <ul><li>[Any of the following] + G r oo v y = success! </li></ul><ul><li>Web testing -> Canoo WebTest </li></ul><ul><li>Swing testing -> FEST </li></ul><ul><li>BDD testing -> Easyb </li></ul>
  28. 28. FEST + Easyb <ul><ul><li>Demo </li></ul></ul>
  29. 29. For More Information <ul><li>h tt p : // groovy.codehaus.org </li></ul><ul><li>h tt p : // junit.org </li></ul><ul><li>h tt p : // testng.org </li></ul><ul><li>h tt p : // dbunit.org </li></ul><ul><li>h tt p : // easytesting.org (FEST)‏ </li></ul><ul><li>h tt p : // easyb.org </li></ul><ul><li>h tt p : // groovy.codehaus.org / Proxy-o-Matic </li></ul><ul><li>h tt p : // groovy.codehaus.org / ObjectGraphBuilder </li></ul><ul><li>h tt p : // jroller.com / aalmiray </li></ul><ul><li>h tt p : // twitter.com / aalmiray </li></ul>
  30. 30. Q & A
  31. 31. Thank You!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×