Svcc Groovy Testing

  • 1,798 views
Uploaded on

Boosting your Testing Productivity with Groovy

Boosting your Testing Productivity with Groovy

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,798
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
31
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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