31
@ Andres Almiray 2008 Silicon Valley Code Camp 2008 Boosting your testing productivity with Groovy Andres Almiray

Svcc Groovy Testing

Embed Size (px)

DESCRIPTION

Boosting your Testing Productivity with Groovy

Citation preview

Page 1: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Boosting your testing productivity with Groovy

Andres Almiray

Page 2: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Have fun while programming tests

Goal

Page 3: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Agenda

• What is Groovy

• Groovy + Testing Frameworks

• Mocking with Groovy

• Testing Databases

• Building Test Data

• Functional UI Testing

• Resources

Page 4: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

What is Groovy?

Page 5: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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() ); }}

Page 6: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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() ); }}

Page 7: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Equivalent HelloWorld 100% Groovy

class HelloWorld { String name def greet() { "Hello $name" }}

def helloWorld = new HelloWorld(name:"Groovy")println helloWorld.greet()

Page 8: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

1st Mantra

Java is Groovy, Groovy is Java

•Every single Java class is a Groovy class, the inverse is also true. This means your Java can call my Groovy in vice versa, without any clutter nor artificial bridge.

•Groovy has the same memory and security models as Java.

•Almost 98% Java code is Groovy code, meaning you can in most cases rename *.java to *.groovy and it will work.

Page 9: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Common Gotchas

• Java Array initializers are not supported, but lists can be coerced into arrays.

• Inner class definitions are not supported (yet!!).

Page 10: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

2nd Mantra

Groovy is Java and Groovy 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.

•Groovy 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!

Page 11: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Groovy + Testing Frameworks

• Any Groovy script may become a testcase• assert keyword enabled by default

• Groovy provides a GroovyTestCase base class• Easier to test exception throwing code

• Junit 4.x and TestNG ready, Groovy supports JDK5+ features• Annotations

• Static imports

• Enums

Page 12: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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 }

}}

Page 13: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Testing exceptions in Groovy

class GroovyExceptionTestCase extends GroovyTestCase { void testExceptionThrowingCode() { shouldFail( UnsupportedOperationException ){ new MyService().doSomething() } }}

Page 14: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

But how do I run Groovy tests?

• Pick your favourite IDE! • IDEA

• Eclipse

• NetBeans

• Command line tools• Ant

• Gant

• Maven

• Good ol’ Groovy shell/console

Page 15: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Mocking with Groovy

• 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 Groovy)

Page 16: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Dynamic Proxies

This feature allows you to define a java.lang.reflect.Proxy using either a Closure or Map + Groovy 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

Page 17: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Dynamic Proxies with Proxy-o-Matic

Proxy-o-Matic extends Groovy'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

Page 18: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Dynamic Proxies

Demo

Page 19: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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 Groovy and Java collaborators, caller must be Groovy though.

Page 20: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Groovy Mocks

Demo

Page 21: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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

Page 22: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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="Acme"/> <employee name="Duke" company_id="1"> </dataset> """ db.dataset = new FlatXmlDataSet( new StringReader(dataset) ) db.onSetUp() }

@AfterClass void exit() { db.onTearDown() }}

Page 23: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Compile-checked datasetimport org.dbunit.*import org.junit.*Import groovy.xml.MarkupBuilder

class MyDBTestCase { IDatabaseTester db

@BeforeClass void init(){ db = new JdbcDatabaseTester("org.hsqldb.jdbcDriver", "jdbc:hsqldb:sample", "sa", "" ) // insert table schema def dataset = new MarkupBuilder().dataset { company( name: Acme ) employee( name: "Duke", company_id: 1 ) } db.dataset = new FlatXmlDataSet( new StringReader(dataset) ) db.onSetUp() }

@AfterClass void exit() { db.onTearDown() }}

Page 24: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Building Test Data

Builders are a great option for creating hierarchical data

MarkupBuilder -> HTML/XML

ObjectGraphBuilder -> bean graph

Page 25: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

ObjectGraphBuilder

Demo

Page 26: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

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

Page 27: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Groovy to the rescue!

• [Any of the following] + Groovy = success!

• Web testing -> Canoo WebTest

• Swing testing -> FEST

• BDD testing -> Easyb

Page 28: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

FEST + Easyb

Demo

Page 29: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

For More Information

http://groovy.codehaus.org

http://junit.org http://testng.org http://dbunit.org http://easytesting.org (FEST) http://easyb.org http://groovy.codehaus.org/Proxy-o-Matic http://groovy.codehaus.org/ObjectGraphBuilder

http://jroller.com/aalmiray http://twitter.com/aalmiray

Page 30: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Q & A

Page 31: Svcc Groovy Testing

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Thank You!