SlideShare a Scribd company logo
1 of 43
Test-Driven in Groovy Joseph Muraski Christopher Bartling
Joseph Muraski Independent Consultant in the Twin Cities Develop enterprise applications using Java, .Net, Groovy, Grails… Email: joe.muraski@mantacs.com Twitter: jmuraski Blog: joemuraski.blogspot.com
Christopher Bartling Independent consultant, based in the Twin Cities Teach, mentor and coach for DevJam Experiences include building enterprise applications with Java, Groovy, Grails, .NET, and Adobe Flex Email: chris.bartling@gmail.com Twitter: cbartling Blog: bartling.blogspot.com
Goals of the workshop Introduce features in Groovy that will help your Java testing efforts Introduce Cucumber and cuke4duke as new tools in your testing arsenal   Demonstrate unit, integration, and acceptance testing with Groovy and Cucumber (via cuke4duke) via an example Java web application Get hands on with these technologies
Summary Design principles Testing facilities built into Groovy Acceptance/BDD testing with Groovy	 Cucumber and cuke4duke Presentation and code available   http://bitbucket.org/joe.muraski/grails_tdd_workshop/ Instructions on how to use Mercurial to clone the repository or retrieve everything in an archive file
Prerequisites for hands on labs Java 1.6 JDK installation Maven 2  That’s it…Maven will take care of resolving all the other dependencies
Maven 2 Maven2 installation required We’re using version 2.2.1 This is not a Maven presentation We use it because it’s quite handy and does a great job of resolving dependencies Maven commands will be presented when needed
Maven plugins used org.codehaus.gmaven:gmaven-plugin:1.2 org.mortbay.jetty:maven-jetty-plugin cuke4duke:cuke4duke-maven-plugin:0.2.4 org.apache.maven.plugins:maven-surefire-plugin
Sample web application Starting the web app mvn jetty:run-exploded Running tests mvn tests Running cuke4duke mvn cuke4duke:cucumber
Design principles Single responsibility per class (high cohesion) Loose coupling of collaborators (low coupling) Injection of dependencies
Why Groovy? Groovy works seamlessly with Java It’s all just bytecode in the end Groovy offers a relaxed Java syntax Interesting tools included in Groovy	 GSQL XmlSlurper, XmlParser, Builders GPath Convenient collections Private method testing
GroovyTestCase Included in Groovy distribution Extends junit.framework.TestCase Provides a number of helper methods for assertions  Does not require test* methods to be void return type No need to declared throws checked exceptions in tests Groovy converts checked exceptions to unchecked exceptions
Convenience assert methods assertArrayEquals(Object[] expected, Object[] actual) assertContains(char expected, char[] actualArray) assertContains(int expected, int[] actualArray) assertInspect(Object value, String expected) assertLength(int length, Object[] array) assertScript(String script) assertToString(Object value, String expected)
shouldFail() closures shouldFail(Closure closure) Asserts supplied closure fails when evaluated shouldFail(Class desiredThrownType, Closure closure) Asserts supplied closure fails when evaluated and particular exception type is thrown shouldFailWithCause(Class desiredCauseType, Closure closure) Will check for a checked exception as the root cause (checked exceptions are wrapped in Groovy)
Mocking Java classes with Groovy Map coercion Groovy’s mock library GMock library Java mocking frameworks
Map coercion Add named closures to the map Only one closure can be mapped to a “method name” Cannot overload methods Coerce to desire type using the as operator
Map coercion example
Private Method Calling Groovy can call private methods Broken encapsulation?   Better reflection abstractions? Java can invoke private methods, it’s just more painful Is testing private methods Good or a Smell?
Private method testing example
Groovy mocking library Groovy's built-in mock support does not allow us to test Java classes Relies on hooking into Groovy's object lifecycle mechanisms Java can not make call to Groovy Mock or Stub Use Java mocking frameworks instead Can cheat by putting Groovy Mock in a coerced map but why??
GMock Groovy-based mock objects framework Easy syntax and usage Works when called by Java classes
GMock Create Mock AddressService service = mock(AddressService) Create Expectation service.fetch(“id”).returns(new Address()).once() Easy Mathcing service.save(match{it.personId == “id”}).returns(“id”).once()
GMock example
Java Mocking Frameworks Java Mocking frameworks can be used with Groovy Some have minor  syntax issues or needed work arounds (JMock) Great to use if you already have one you are using and don’t want to switch
GSQL Easy to create connections sql= Sql.newInstance("jdbc:hsqldb:hsql://localhost/", "sa", "", "org.hsqldb.jdbcDriver") Simple to work with row sets sql.rows(“select * from address”).each {println “id: ${it.id}”} No try catch blocks
GSQL example
Acceptance Test-Driven Story tests manifest themselves as executable tests Drives the development of complete features Frameworks are available Fit, FitLibrary, FitNesse http://fit.c2.com/ Robot Framework  http://code.google.com/p/robotframework/ Cucumber  http://cukes.info/ Others
Cucumber Tool for executing plain-text functional descriptions as automated tests Supports BDD Cucumber tests are typically written before anything else verified by business analysts, domain experts, non-technical stakeholders  The production code is then written to make the Cucumber tests pass Outside-in development
Cucumber (via cuke4duke) Cucumber for the JVM Outside-in testing Facilitates automation of acceptance/story tests Features and scenarios use normal language Step definitions can be written in Groovy, as we’ll see Uses JRuby to run the core Cucumber framework http://wiki.github.com/aslakhellesoy/cuke4duke/
Cucumber features Purposes Documentation of the system Automated, executable acceptance tests What code should I write next?  Written in Gherkin Business readable DSL Describe software behavior without detailing implementation Grammar exists in different spoken languages (37 currently) Feature source files have .feature extension
Given-When-Then Scenarios consist of steps Given: Put the system in a known state before the user starts interacting with the system; pre-conditions When: Describe the key action a user performs in this scenario Then: Observe outcomes; observations should relate to business value of the feature Use And and But to keep features fluent
Cucumber step definitions Written in Groovy for our examples Can be written in many different programming languages Analogous to method or function definition Start with adjective/adverb Regular expression which will match some text from a feature(s) Take 0 or more arguments Arguments bound to regular expression groups Multi-line step arguments
Hooks Before Executes before the first step of each scenario After Executes after the last step of each scenario Execute regardless if there are failing, undefined, pending or skipped steps AfterStep Executes after each step in a scenario
Hooks Found in Groovy files in the support directory Our example uses one: env.groovy Hooks allow us to run blocks of code at various points in the Cucumber test cycle No association between where the hook is defined and which scenario/step it is run for All defined hooks (one or more) are run whenever the relevant event occurs
Tagged hooks Need to execute a certain hook for only certain scenarios Achieved by associating a Before, After or AfterStep hook with one or more tags See the env.groovy file It uses a Before hook with a @database tag to load the database via DBUnit
Tagging scenarios Allows you to group scenarios for inclusion or exclusion in a Cucumber run @wip is provided out of the box Using tags in cuke4duke and Maven… mvn cuke4duke:cucumber –DcukeArgs=“--tags @wip” mvn cuke4duke:cucumber -DcukeArgs=“--tags ~@wip” Inside of the Maven POM: <cucumberArg>${cukeArgs}</cucumberArg>
Running cuke4duke The cuke4duke Maven plugin mvn cuke4duke:cucumber First time: use –Dcucumber.installGems=true Add –o option to work offline Features and step definitions are discovered by the plugin Features belong in features directory Step definitions found in the step_definitions directory
cuke4duke acceptance tests Step definitions will be written in Groovy Other JVM languages are allowed Use cuke4duke.GroovyDsl Use WebDriver: http://code.google.com/p/selenium Test web UI Use DbUnit: http://www.dbunit.org/ Bulk load the database with known fixture data
Acceptance testing configuration
Cucumber examples
Workshop activities Now it’s your turn! Try your hand at unit testing with Groovy Refactor the existing web app Introduce DAO for database functionality Add services to orchestrate business logic Write some Cucumber features and build out or reuse the Groovy step definitions Try to add new features and practice outside-in development Is ATDD beneficial?  Why or why not?
Interesting resources http://cukes.info/ http://blog.dannorth.net/whats-in-a-story/ http://blog.dannorth.net/introducing-bdd/ http://www.ibm.com/developerworks/java/library/j-pg11094/ http://wiki.github.com/aslakhellesoy/cucumber/
Discussion

More Related Content

What's hot

Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyonddn
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailszenMonkey
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Robert Stern
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHarry Potter
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsYura Nosenko
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldYura Nosenko
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and PythonAndreas Schreiber
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golangYoni Davidson
 
Future-proof your Go API - GothamGo 2018
Future-proof your Go API - GothamGo 2018Future-proof your Go API - GothamGo 2018
Future-proof your Go API - GothamGo 2018Steve Kaliski
 
Solr onfitnesse learningfromberlinbuzzwords
Solr onfitnesse learningfromberlinbuzzwordsSolr onfitnesse learningfromberlinbuzzwords
Solr onfitnesse learningfromberlinbuzzwordsDmitry Kan
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnitGreg.Helton
 
Dead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with CoroutinesDead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with CoroutinesTravis Kaufman
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)jaxLondonConference
 

What's hot (20)

Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Doing the Impossible
Doing the ImpossibleDoing the Impossible
Doing the Impossible
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and Python
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
 
Future-proof your Go API - GothamGo 2018
Future-proof your Go API - GothamGo 2018Future-proof your Go API - GothamGo 2018
Future-proof your Go API - GothamGo 2018
 
What is new in JUnit5
What is new in JUnit5What is new in JUnit5
What is new in JUnit5
 
Solr onfitnesse learningfromberlinbuzzwords
Solr onfitnesse learningfromberlinbuzzwordsSolr onfitnesse learningfromberlinbuzzwords
Solr onfitnesse learningfromberlinbuzzwords
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
 
Dead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with CoroutinesDead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with Coroutines
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
 

Similar to Test Driven In Groovy

Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyJames Williams
 
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovygreach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovyJessie Evangelista
 
Groovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web ApplicationsGroovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web Applicationsrohitnayak
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...Guillaume Laforge
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneAndres Almiray
 
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-AppsSelenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-Appschrisb206 chrisb206
 
Acceptance testing with Geb
Acceptance testing with GebAcceptance testing with Geb
Acceptance testing with GebRichard Paul
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebC4Media
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersKostas Saidis
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF SummitOrtus Solutions, Corp
 
Embedding Groovy in a Java Application
Embedding Groovy in a Java ApplicationEmbedding Groovy in a Java Application
Embedding Groovy in a Java ApplicationPaolo Predonzani
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - Ortus Solutions, Corp
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION APIGavin Pickin
 
JavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderJavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderAndres Almiray
 
Testing frontends with nightwatch & saucelabs
Testing frontends with nightwatch & saucelabsTesting frontends with nightwatch & saucelabs
Testing frontends with nightwatch & saucelabsTudor Barbu
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJSPeter Drinnan
 

Similar to Test Driven In Groovy (20)

Svcc Groovy Testing
Svcc Groovy TestingSvcc Groovy Testing
Svcc Groovy Testing
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovygreach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
 
Groovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web ApplicationsGroovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web Applications
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-AppsSelenium-Browser-Based-Automated-Testing-for-Grails-Apps
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
 
Acceptance testing with Geb
Acceptance testing with GebAcceptance testing with Geb
Acceptance testing with Geb
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
 
Embedding Groovy in a Java Application
Embedding Groovy in a Java ApplicationEmbedding Groovy in a Java Application
Embedding Groovy in a Java Application
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
 
JavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderJavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilder
 
Testing frontends with nightwatch & saucelabs
Testing frontends with nightwatch & saucelabsTesting frontends with nightwatch & saucelabs
Testing frontends with nightwatch & saucelabs
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 

More from Christopher Bartling

JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma Christopher Bartling
 
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmChristopher Bartling
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaChristopher Bartling
 
Acceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsAcceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsChristopher Bartling
 
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformChristopher Bartling
 

More from Christopher Bartling (12)

JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvm
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
Building Tropo Apps with Grails
Building Tropo Apps with GrailsBuilding Tropo Apps with Grails
Building Tropo Apps with Grails
 
CoffeeScript By Example
CoffeeScript By ExampleCoffeeScript By Example
CoffeeScript By Example
 
Acceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsAcceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And Friends
 
Introduction To Grails
Introduction To GrailsIntroduction To Grails
Introduction To Grails
 
Cucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and GroovyCucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and Groovy
 
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer Platform
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Grails Overview
Grails OverviewGrails Overview
Grails Overview
 
Rich Web Clients 20081118
Rich Web Clients 20081118Rich Web Clients 20081118
Rich Web Clients 20081118
 

Test Driven In Groovy

  • 1. Test-Driven in Groovy Joseph Muraski Christopher Bartling
  • 2. Joseph Muraski Independent Consultant in the Twin Cities Develop enterprise applications using Java, .Net, Groovy, Grails… Email: joe.muraski@mantacs.com Twitter: jmuraski Blog: joemuraski.blogspot.com
  • 3. Christopher Bartling Independent consultant, based in the Twin Cities Teach, mentor and coach for DevJam Experiences include building enterprise applications with Java, Groovy, Grails, .NET, and Adobe Flex Email: chris.bartling@gmail.com Twitter: cbartling Blog: bartling.blogspot.com
  • 4. Goals of the workshop Introduce features in Groovy that will help your Java testing efforts Introduce Cucumber and cuke4duke as new tools in your testing arsenal Demonstrate unit, integration, and acceptance testing with Groovy and Cucumber (via cuke4duke) via an example Java web application Get hands on with these technologies
  • 5. Summary Design principles Testing facilities built into Groovy Acceptance/BDD testing with Groovy Cucumber and cuke4duke Presentation and code available http://bitbucket.org/joe.muraski/grails_tdd_workshop/ Instructions on how to use Mercurial to clone the repository or retrieve everything in an archive file
  • 6. Prerequisites for hands on labs Java 1.6 JDK installation Maven 2 That’s it…Maven will take care of resolving all the other dependencies
  • 7. Maven 2 Maven2 installation required We’re using version 2.2.1 This is not a Maven presentation We use it because it’s quite handy and does a great job of resolving dependencies Maven commands will be presented when needed
  • 8. Maven plugins used org.codehaus.gmaven:gmaven-plugin:1.2 org.mortbay.jetty:maven-jetty-plugin cuke4duke:cuke4duke-maven-plugin:0.2.4 org.apache.maven.plugins:maven-surefire-plugin
  • 9. Sample web application Starting the web app mvn jetty:run-exploded Running tests mvn tests Running cuke4duke mvn cuke4duke:cucumber
  • 10. Design principles Single responsibility per class (high cohesion) Loose coupling of collaborators (low coupling) Injection of dependencies
  • 11. Why Groovy? Groovy works seamlessly with Java It’s all just bytecode in the end Groovy offers a relaxed Java syntax Interesting tools included in Groovy GSQL XmlSlurper, XmlParser, Builders GPath Convenient collections Private method testing
  • 12. GroovyTestCase Included in Groovy distribution Extends junit.framework.TestCase Provides a number of helper methods for assertions Does not require test* methods to be void return type No need to declared throws checked exceptions in tests Groovy converts checked exceptions to unchecked exceptions
  • 13. Convenience assert methods assertArrayEquals(Object[] expected, Object[] actual) assertContains(char expected, char[] actualArray) assertContains(int expected, int[] actualArray) assertInspect(Object value, String expected) assertLength(int length, Object[] array) assertScript(String script) assertToString(Object value, String expected)
  • 14. shouldFail() closures shouldFail(Closure closure) Asserts supplied closure fails when evaluated shouldFail(Class desiredThrownType, Closure closure) Asserts supplied closure fails when evaluated and particular exception type is thrown shouldFailWithCause(Class desiredCauseType, Closure closure) Will check for a checked exception as the root cause (checked exceptions are wrapped in Groovy)
  • 15. Mocking Java classes with Groovy Map coercion Groovy’s mock library GMock library Java mocking frameworks
  • 16. Map coercion Add named closures to the map Only one closure can be mapped to a “method name” Cannot overload methods Coerce to desire type using the as operator
  • 18. Private Method Calling Groovy can call private methods Broken encapsulation? Better reflection abstractions? Java can invoke private methods, it’s just more painful Is testing private methods Good or a Smell?
  • 20. Groovy mocking library Groovy's built-in mock support does not allow us to test Java classes Relies on hooking into Groovy's object lifecycle mechanisms Java can not make call to Groovy Mock or Stub Use Java mocking frameworks instead Can cheat by putting Groovy Mock in a coerced map but why??
  • 21. GMock Groovy-based mock objects framework Easy syntax and usage Works when called by Java classes
  • 22. GMock Create Mock AddressService service = mock(AddressService) Create Expectation service.fetch(“id”).returns(new Address()).once() Easy Mathcing service.save(match{it.personId == “id”}).returns(“id”).once()
  • 24. Java Mocking Frameworks Java Mocking frameworks can be used with Groovy Some have minor syntax issues or needed work arounds (JMock) Great to use if you already have one you are using and don’t want to switch
  • 25. GSQL Easy to create connections sql= Sql.newInstance("jdbc:hsqldb:hsql://localhost/", "sa", "", "org.hsqldb.jdbcDriver") Simple to work with row sets sql.rows(“select * from address”).each {println “id: ${it.id}”} No try catch blocks
  • 27. Acceptance Test-Driven Story tests manifest themselves as executable tests Drives the development of complete features Frameworks are available Fit, FitLibrary, FitNesse http://fit.c2.com/ Robot Framework http://code.google.com/p/robotframework/ Cucumber http://cukes.info/ Others
  • 28. Cucumber Tool for executing plain-text functional descriptions as automated tests Supports BDD Cucumber tests are typically written before anything else verified by business analysts, domain experts, non-technical stakeholders The production code is then written to make the Cucumber tests pass Outside-in development
  • 29. Cucumber (via cuke4duke) Cucumber for the JVM Outside-in testing Facilitates automation of acceptance/story tests Features and scenarios use normal language Step definitions can be written in Groovy, as we’ll see Uses JRuby to run the core Cucumber framework http://wiki.github.com/aslakhellesoy/cuke4duke/
  • 30. Cucumber features Purposes Documentation of the system Automated, executable acceptance tests What code should I write next? Written in Gherkin Business readable DSL Describe software behavior without detailing implementation Grammar exists in different spoken languages (37 currently) Feature source files have .feature extension
  • 31. Given-When-Then Scenarios consist of steps Given: Put the system in a known state before the user starts interacting with the system; pre-conditions When: Describe the key action a user performs in this scenario Then: Observe outcomes; observations should relate to business value of the feature Use And and But to keep features fluent
  • 32. Cucumber step definitions Written in Groovy for our examples Can be written in many different programming languages Analogous to method or function definition Start with adjective/adverb Regular expression which will match some text from a feature(s) Take 0 or more arguments Arguments bound to regular expression groups Multi-line step arguments
  • 33. Hooks Before Executes before the first step of each scenario After Executes after the last step of each scenario Execute regardless if there are failing, undefined, pending or skipped steps AfterStep Executes after each step in a scenario
  • 34. Hooks Found in Groovy files in the support directory Our example uses one: env.groovy Hooks allow us to run blocks of code at various points in the Cucumber test cycle No association between where the hook is defined and which scenario/step it is run for All defined hooks (one or more) are run whenever the relevant event occurs
  • 35. Tagged hooks Need to execute a certain hook for only certain scenarios Achieved by associating a Before, After or AfterStep hook with one or more tags See the env.groovy file It uses a Before hook with a @database tag to load the database via DBUnit
  • 36. Tagging scenarios Allows you to group scenarios for inclusion or exclusion in a Cucumber run @wip is provided out of the box Using tags in cuke4duke and Maven… mvn cuke4duke:cucumber –DcukeArgs=“--tags @wip” mvn cuke4duke:cucumber -DcukeArgs=“--tags ~@wip” Inside of the Maven POM: <cucumberArg>${cukeArgs}</cucumberArg>
  • 37. Running cuke4duke The cuke4duke Maven plugin mvn cuke4duke:cucumber First time: use –Dcucumber.installGems=true Add –o option to work offline Features and step definitions are discovered by the plugin Features belong in features directory Step definitions found in the step_definitions directory
  • 38. cuke4duke acceptance tests Step definitions will be written in Groovy Other JVM languages are allowed Use cuke4duke.GroovyDsl Use WebDriver: http://code.google.com/p/selenium Test web UI Use DbUnit: http://www.dbunit.org/ Bulk load the database with known fixture data
  • 41. Workshop activities Now it’s your turn! Try your hand at unit testing with Groovy Refactor the existing web app Introduce DAO for database functionality Add services to orchestrate business logic Write some Cucumber features and build out or reuse the Groovy step definitions Try to add new features and practice outside-in development Is ATDD beneficial? Why or why not?
  • 42. Interesting resources http://cukes.info/ http://blog.dannorth.net/whats-in-a-story/ http://blog.dannorth.net/introducing-bdd/ http://www.ibm.com/developerworks/java/library/j-pg11094/ http://wiki.github.com/aslakhellesoy/cucumber/

Editor's Notes

  1. NEW
  2. Behaviour-driven development is an “outside-in” methodology. It starts at the outside by identifying business outcomes, and then drills down into the feature set that will achieve those outcomes. Each feature is captured as a “story”, which defines the scope of the feature along with its acceptance criteria. This article introduces the BDD approach to defining and identifying stories and their acceptance criteria.