Boosting Your Testing Productivity with Groovy

25
Boosting your testing productivity with Groovy James Williams, Spatial Networks Andres Almiray, Oracle BOF-5101

description

 

Transcript of Boosting Your Testing Productivity with Groovy

Page 1: Boosting Your Testing Productivity with Groovy

Boosting your testing productivity with Groovy

James Williams, Spatial NetworksAndres Almiray, Oracle

BOF-5101

Page 2: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 2

Have fun while programming tests

Page 3: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 3

Agenda

What is GroovyGroovy + Testing FrameworksMocking with GroovyGrails TestingTesting DatabasesFunctional TestingResources

Page 4: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 4

What is Groovy?

Groovy is an agile and dynamic language for the Java Virtual Machine Builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby & Smalltalk Makes modern programming features available to Java developers with almost-zero learning curveSupports Domain Specific Languages and other compact syntax so your code becomes easy to read and maintain

Page 5: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 5

What is Groovy?

Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications Simplifies testing by supporting unit testing and mocking out-of-the-box Seamlessly integrates with all existing Java objects and libraries Compiles straight to Java byte code so you can use it anywhere you can use Java

Page 6: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 6

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 7: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 7

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 8: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 8

Equivalent HelloWorld 100% Groovy

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

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

Page 9: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 9

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 suports JDK5+ features• Annotations• Static imports• Enums

Page 10: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 10

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 11: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 11

Testing exceptions in Groovy

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

Page 12: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 12

Alright, but how do I run Groovy tests?

Pick your favorite IDE! • IDEA• Eclipse• NetBeans

Command line tools• Ant• Gant• Maven• Good ol’ Groovy shell/console

Page 13: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 13

Mocking with Groovy

Known mocking libraries• EasyMock – record/replay• Jmock – write expectations as you go

Use dynamic proxies as stubs

Use StubFor/MockFor• Groovy mocks rely on MetaClasses to do their magic• Caveat: use them only for Groovy classes

Page 14: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 14

Groovy Mocks

Page 15: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 15

Grails testing

Fairly similar to testing in core GroovyPlugins a plenty• easyb• Canoo WebTest• Selenium• DBUnit• jsUnit• Cobertura• JUnit is built in

Page 16: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 16

Testing Taglibs

Can be tested with JunitNeed to extend several metaClasses that are not in testing scope• out• Encoders/Decoders

General format: className.tagName( [ attrs as Map], body)

Page 17: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 17

Grails Taglibs

Page 18: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 18

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 19: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 19

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", "" ) 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 20: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 20

Compile-checked dataset

import 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", "" ) 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 21: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 21

Functional Testing

These tests usually require more setupNon-developers usually like to drive these testsDevelopers usually don’t like to code these testsNo Functional Testing => unhappy customer => unhappy developer

Page 22: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 22

Groovy to the rescue!

[Any of the following] + Groovy = success!

Web testing -> Canoo WebTest

Swing testing -> FEST

BDD testing -> Easyb

Page 23: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 23

FEST + Easyb

Page 24: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 24

For More Information

http://groovy.codehaus.orghttp://grails.org

http://junit.orghttp://testng.orghttp://www.dbunit.orghttp://webtest.canoo.comhttp://easyb.orghttp://easytesting.org

http://jameswilliams.behttp://jroller.com/aalmiray

Page 25: Boosting Your Testing Productivity with Groovy

2008 JavaOneSM Conference | java.com.sun/javaone | 25

James Williams, Spatial NetworksAndres Almiray, Oracle

BOF-5101