Java Testing With Spock - Ken Sipe (Trexin Consulting)

3,665 views
3,367 views

Published on

The Spock unit testing framework is on the verge of a 1.0 release and has already proven itself to be the next generation thinking on how to test Java production code. One of the many ever present challenges to testing code is the ability to Mock classes which has simplified by Spock from a very early release. Recently added to Spock is the notion of Stubs and Spies. This sessions is designed to demonstrate proper unit testing technique showing off these new features along with a number of advanced Spock features.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,665
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
50
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Java Testing With Spock - Ken Sipe (Trexin Consulting)

  1. 1. NFJS Software Symposium Series 2013 Spock - The Logical Enterprise Testing Tool Ken Sipe
  2. 2. About Speaker Spock Testing http://kensipe.blogspot.com/ http://del.icio.us/kensipe twitter: @kensipe ken.sipe@gmail.com Developer: Embedded, C++, Java, Groovy, Grails, C#, Objective C Speaker: JavaOne 2009 Rock Star, NFJS, JAX Microsoft MCP Sun Certified Java 2 Architect Master of Scrums Agile Coach Instructor: VisiBroker CORBA Rational Rose, OOAD
  3. 3. Agenda Spock Testing n Testing n Introduction n Mocking to Spock
  4. 4. Spock Testing Spock Intro 4
  5. 5. Spock is... n testing framework... n based Spock Testing on Groovy n fully compatible with JUnit n result of learnings from ¨RSpec, BDD, JUnit 5
  6. 6. Spock Can... n Reduces n Make n Be Spock Testing lines of code tests more readable extended 6
  7. 7. Spock Testing JUnit 7
  8. 8. Taxonomy of JUnit Spock Testing
  9. 9. Spock Testing JUnit Pain Points? 9
  10. 10. Unit testing needs Spock Testing n Concise, Clear and Readable n Promote “user” thinking ¨context ¨stimulus ¨expectations n Productivity 10
  11. 11. Spock Testing Groovy 11
  12. 12. Taxonomy of GroovyTestCase Spock Testing
  13. 13. Groovy and Testing Spock Testing
  14. 14. Groovy Power Assert Condition not satisfied: interest == calc.calculate(amt, year) | | | | | | 25.0 | | 20.0 100 4 | com.math.SimpleInterestCalculator@606e1dec false Spock Testing
  15. 15. Groovy Improvements Spock Testing n Concise, Clear and Readable n Promote “user” thinking ¨context ¨stimulus ¨expectations n Productivity 15
  16. 16. Spock Testing Spock 16
  17. 17. Spock Spock Testing n Programmers Environment ¨Groovy n Promotes Clarity ¨structural blocks ¨removes noise 17
  18. 18. Why use Spock? Spock Testing n Expressive testing language n Easy to learn n Usable from unit to end-to-end n Leverages Groovy n Runs with JUnit Runner ¨IDE ¨CI 18
  19. 19. Spock Testing Taxonomy of a Spec 19
  20. 20. First Spec Spock Testing
  21. 21. Terms Spock Testing n Specification ¨compare to TestCase or GroovyTestCase ¨Instructs JUnit to run with Sputnik (JUnit runner) n Fields ¨initialized for each “test” ¨think “setup” ¨not shared between feature methods 21
  22. 22. More Fields Spock Testing n Shared ¨Setup once ¨think setupSpec() n statics ¨only use for constants 22
  23. 23. Fixture Methods Spock Testing n before / after a feature n before / after a spec n optional 23
  24. 24. Feature Methods Spock Testing n “heart” of spec n four phases ¨setup the features fixture ¨provide stimulus to system ¨describes the response ¨clean up 24
  25. 25. Blocks Spock Testing given: preconditions, data fixtures when: actions that trigger some outcome then: makes assertions about outcome expect: short alt to when & then where: applies varied inputs and: sub-divides other blocks setup: alias for given cleanup: post-conditions, housekeeping 25
  26. 26. Common Blocks Spock Testing n When / Then / Where n Given / When / Then 26
  27. 27. Blocks Spock Testing n setup ¨must be first ¨must be the only ¨no special semantics ¨label is optional ¨label given: is an alias 27
  28. 28. When / Then Blocks n used together ¨ possible n then Spock Testing to have many per feature restrictions ¨ conditions ¨ exception conditions ¨ automatic asserts ¨ interactions ¨ variable defs 28
  29. 29. Conditions n checking Spock Testing for exceptions 29
  30. 30. Throwing an Exception NOT! Spock Testing 30
  31. 31. Mocks and Expectations Spock Testing 31
  32. 32. Cleanup on Feature X n cleanup Spock Testing block ¨only followed by a where block ¨no repeats 32
  33. 33. Where Blocks Spock Testing ¨last in a method ¨no repeats ¨used for data-driven features 33
  34. 34. Helper Methods ¨either Spock Testing return a boolean n or ¨assert 34
  35. 35. Spock Testing Specification Functions and Spock.lang.* 35
  36. 36. Spock Testing n old() n thrown() n with / notThrown {} 36
  37. 37. Spock.lang.* Documentation Spock Testing @Title @Narrative @Issue @See @Subject 37
  38. 38. Spock.lang.* Ignoring Stuff Spock Testing @Requires @IgnoreIf @Ignore @IgnoreRest 38
  39. 39. Reporting Spock Testing @Unroll 39
  40. 40. Built-in Extensions: AutoCleanup Spock Testing @AutoCleanup @AutoCleanup(‘dispose’) @AutoCleanup(quite=true) 40
  41. 41. Built-in Extensions: Timeout Spock Testing @Timeout @Timeout(10) @Timeout(value=10, unit=TimeUnit.MILLISECONDS) 41
  42. 42. Built-in Extensions: Stepwise Spock Testing @Stepwise 42
  43. 43. Spock Testing Hamcrest 43
  44. 44. Spock Testing JUnit Rules 44
  45. 45. Spock Testing SpockConfig 45
  46. 46. References n Getting Spock Testing Spock ¨http://code.google.com/p/spock/ n Source from Presentation ¨https://github.com/kensipe/spock-demos-nfjs 46
  47. 47. Summary n Closing and Q&A ¨ Please fill out the session evaluation ¨ Ken Sipe ken.sipe@gmail.com n kensipe.blogspot.com n twitter: @kensipe n Spock Testing

×