Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing Java using Groovy and Mock Objects Walker Hale Human Genome Sequencing Center Baylor College of Medicine.

Similar presentations


Presentation on theme: "Unit Testing Java using Groovy and Mock Objects Walker Hale Human Genome Sequencing Center Baylor College of Medicine."— Presentation transcript:

1 Unit Testing Java using Groovy and Mock Objects Walker Hale Human Genome Sequencing Center Baylor College of Medicine

2 The Plan Groovy makes testing Java better. Motivation Introduce Groovy Apply Groovy to Testing Mock Objects TestNG Grand Demo

3 The Setting Java shop Subversion — for version control Maven 2 — for most builds Groovy — for various tasks Many apps running against a common database and set of libraries

4 An Ideal Deliverable in Our Shop Code checked into version control Builds with maven 2 Passes all unit tests $ svn checkout \ $repo/my-project/trunk \ my-project $ cd my-project $ mvn test

5 The Plot The Motivation –More unit testing! The Insight –Small changes in the difficulty or unpleasantness of a task produce disproportionately large changes in how much that task is performed. Enter Groovy

6 Groovy in 15 Minutes Almost all Java code is Groovy code –no inner classes –http://groovy.codehaus.org/Differences+from+Jav a Groovy adds: –Fun syntactic sugar –Lazy method dispatch –Methods to the standard Java library classes –Closures –Groovy-specific classes Interactive shell

7 Syntax Sugar // MapSample.java import java.util.*; public class MapSample { public static void main(String[] args) { Map m = new LinkedHashMap (); m.put("red", 1); m.put("blue", 2); System.out.println(m.getClass()); System.out.println(m); } // MapSampleG.groovy def m = [red:1, blue:2] println m.getClass() println m //.toString() for Java-style output

8 Syntax Sugar $ javac MapSample.java $ java MapSample class java.util.LinkedHashMap {red=1, blue=2} $ groovy MapSampleG.groovy class java.util.LinkedHashMap ["red":1, "blue":2] $ groovyc MapSampleG.groovy $ java -cp `pwd`:$GROOVY_EMBED_JAR MapSampleG class java.util.LinkedHashMap ["red":1, "blue":2]

9 Lazy Dispatch & Expanding JDK // DispatchSample.groovy def x = "Hello World" def l = ["abc", "def"] println x.split() l.addAll x.split().toList() println l l << "more" << "stuff" println l $ groovy DispatchSample.groovy {"Hello", "World"} ["abc", "def", "Hello", "World"] ["abc", "def", "Hello", "World", "more", "stuff"]

10 Closures 1 // ClosureSample1.groovy def c = { it + it } println c(2) println c("hello ") def foo(Closure cl) { cl(5, 7) } println foo { x, y -> x + y } println foo { x, y -> x * y } $ groovy ClosureSample1.groovy 4 hello 12 35

11 Closures 2 // ClosureSample2.groovy [2, 3, 5].each { println it * it } [red:1, blue:2].each { key, value -> println key * value } $ groovy ClosureSample2.groovy 4 9 25 red blueblue

12 Closures 3 // ClosureSample3.groovy def strategies = [ add:{ a,b -> a + b }, mult:{ a,b -> a * b } ] [[1,2], [3,4], [5,6]].each { data -> strategies.each { name, code -> printf "%d %4s %2d\n", data, name, code(data) } $ groovy ClosureSample3.groovy [1, 2] add 3 [1, 2] mult 2 [3, 4] add 7 [3, 4] mult 12 [5, 6] add 11 [5, 6] mult 30

13 Testing in Groovy Compiled (not limited) with JUnit 3.8.2 Adds –GroovyTestCase New assertions, such as shouldFail –GroovyTestSuite Simple command line operation URLs –http://groovy.codehaus.org/Testing+Guide –http://groovy.codehaus.org/Unit+Testing

14 Demo Time!

15 Resources http://groovy.codehaus.org/ –Documentation –Differences+from+Java –Testing+Guide http://searchgroovy.org/


Download ppt "Unit Testing Java using Groovy and Mock Objects Walker Hale Human Genome Sequencing Center Baylor College of Medicine."

Similar presentations


Ads by Google