Boosting your testing productivity with Groovy James Williams, Spatial Networks Andres Almiray, Oracle BOF-5101
<ul><ul><li>Have fun while programming tests </li></ul></ul>
Agenda <ul><li>What is Groovy </li></ul><ul><li>Groovy + Testing Frameworks </li></ul><ul><li>Mocking with Groovy </li></u...
What is Groovy? <ul><li>Groovy is an agile and  dynamic  language for the Java Virtual Machine  </li></ul><ul><li>Builds u...
What is Groovy? <ul><li>Increases developer productivity by  reducing scaffolding  code when developing web, GUI, database...
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% Groovy <ul><li>class  HelloWorld { </li></ul><ul><li>String name </li></ul><ul><li>def  greet()...
Groovy + Testing Frameworks <ul><li>Any Groovy script may become a testcase </li></ul><ul><ul><li>assert keyword enabled b...
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 testE...
Alright, but how do I run Groovy tests? <ul><li>Pick your favorite IDE!  </li></ul><ul><ul><li>IDEA </li></ul></ul><ul><ul...
Mocking with Groovy <ul><li>Known mocking libraries </li></ul><ul><ul><li>EasyMock – record/replay </li></ul></ul><ul><ul>...
Groovy Mocks
Grails testing <ul><li>Fairly similar to testing in core Groovy </li></ul><ul><li>Plugins a plenty </li></ul><ul><ul><li>e...
Testing Taglibs <ul><li>Can be tested with Junit </li></ul><ul><li>Need to extend several metaClasses that are not in test...
Grails Taglibs
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...
Functional Testing <ul><li>These tests usually require more setup </li></ul><ul><li>Non-developers usually like to drive t...
Groovy to the rescue! <ul><li>[Any of the following] + Groovy = success! </li></ul><ul><li>Web testing -> Canoo WebTest </...
FEST + Easyb
For More Information <ul><li>http://groovy.codehaus.org </li></ul><ul><li>http://grails.org </li></ul><ul><li>http://junit...
James Williams, Spatial Networks Andres Almiray, Oracle BOF-5101
Upcoming SlideShare
Loading in...5
×

Javaone2008 Bof 5101 Groovytesting

5,371

Published on

Boosting Your Testing Productivity with Groovy

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

No Downloads
Views
Total Views
5,371
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
201
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Javaone2008 Bof 5101 Groovytesting

  1. 1. Boosting your testing productivity with Groovy James Williams, Spatial Networks Andres Almiray, Oracle BOF-5101
  2. 2. <ul><ul><li>Have fun while programming tests </li></ul></ul>
  3. 3. Agenda <ul><li>What is Groovy </li></ul><ul><li>Groovy + Testing Frameworks </li></ul><ul><li>Mocking with Groovy </li></ul><ul><li>Grails Testing </li></ul><ul><li>Testing Databases </li></ul><ul><li>Functional Testing </li></ul><ul><li>Resources </li></ul>
  4. 4. What is Groovy? <ul><li>Groovy is an agile and dynamic language for the Java Virtual Machine </li></ul><ul><li>Builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby & Smalltalk </li></ul><ul><li>Makes modern programming features available to Java developers with almost-zero learning curve </li></ul><ul><li>Supports Domain Specific Languages and other compact syntax so your code becomes easy to read and maintain </li></ul>
  5. 5. What is Groovy? <ul><li>Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications </li></ul><ul><li>Simplifies testing by supporting unit testing and mocking out-of-the-box </li></ul><ul><li>Seamlessly integrates with all existing Java objects and libraries </li></ul><ul><li>Compiles straight to Java byte code so you can use it anywhere you can use Java </li></ul>
  6. 6. 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>
  7. 7. 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>
  8. 8. Equivalent HelloWorld 100% Groovy <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>
  9. 9. Groovy + Testing Frameworks <ul><li>Any Groovy script may become a testcase </li></ul><ul><ul><li>assert keyword enabled by default </li></ul></ul><ul><li>Groovy 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, Groovy suports 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>
  10. 10. 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>
  11. 11. 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>
  12. 12. Alright, but how do I run Groovy tests? <ul><li>Pick your favorite 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’ Groovy shell/console </li></ul></ul>
  13. 13. Mocking with Groovy <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><li>Use StubFor/MockFor </li></ul><ul><ul><li>Groovy mocks rely on MetaClasses to do their magic </li></ul></ul><ul><ul><li>Caveat: use them only for Groovy classes </li></ul></ul>
  14. 14. Groovy Mocks
  15. 15. Grails testing <ul><li>Fairly similar to testing in core Groovy </li></ul><ul><li>Plugins a plenty </li></ul><ul><ul><li>easyb </li></ul></ul><ul><ul><li>Canoo WebTest </li></ul></ul><ul><ul><li>Selenium </li></ul></ul><ul><ul><li>DBUnit </li></ul></ul><ul><ul><li>jsUnit </li></ul></ul><ul><ul><li>Cobertura </li></ul></ul><ul><ul><li>JUnit is built in </li></ul></ul>
  16. 16. Testing Taglibs <ul><li>Can be tested with Junit </li></ul><ul><li>Need to extend several metaClasses that are not in testing scope </li></ul><ul><ul><li>out </li></ul></ul><ul><ul><li>Encoders/Decoders </li></ul></ul><ul><li>General format: </li></ul><ul><ul><li>className.tagName( [ attrs as Map], body) </li></ul></ul>
  17. 17. Grails Taglibs
  18. 18. 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>
  19. 19. 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>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>
  20. 20. 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>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>
  21. 21. Functional 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>
  22. 22. Groovy to the rescue! <ul><li>[Any of the following] + Groovy = 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>
  23. 23. FEST + Easyb
  24. 24. For More Information <ul><li>http://groovy.codehaus.org </li></ul><ul><li>http://grails.org </li></ul><ul><li>http://junit.org </li></ul><ul><li>http://testng.org </li></ul><ul><li>http:// www.dbunit.org </li></ul><ul><li>http:// webtest.canoo.com </li></ul><ul><li>http://easyb.org </li></ul><ul><li>http:// easytesting.org </li></ul><ul><li>http :// jameswilliams.be </li></ul><ul><li>http:// jroller.com/aalmiray </li></ul>
  25. 25. James Williams, Spatial Networks Andres Almiray, Oracle BOF-5101
  1. A particular slide catching your eye?

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

×