Svcc Groovy Testing
-
Upload
andres-almiray -
Category
Technology
-
view
1.814 -
download
0
description
Transcript of Svcc Groovy Testing
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Boosting your testing productivity with Groovy
Andres Almiray
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Have fun while programming tests
Goal
@ 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
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
What is Groovy?
@ 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() ); }}
@ 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() ); }}
@ 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()
@ 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.
@ 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!!).
@ 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!
@ 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
@ 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 }
}}
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Testing exceptions in Groovy
class GroovyExceptionTestCase extends GroovyTestCase { void testExceptionThrowingCode() { shouldFail( UnsupportedOperationException ){ new MyService().doSomething() } }}
@ 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
@ 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)
@ 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
@ 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
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Dynamic Proxies
Demo
@ 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.
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Groovy Mocks
Demo
@ 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
@ 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() }}
@ 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() }}
@ 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
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
ObjectGraphBuilder
Demo
@ 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
@ 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
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
FEST + Easyb
Demo
@ 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
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Q & A
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Thank You!