Java Testing With Spock - Ken Sipe (Trexin Consulting)

  • 2,002 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,002
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
18
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. NFJS Software Symposium Series 2013 Spock - The Logical Enterprise Testing Tool Ken Sipe
  • 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. Agenda Spock Testing n Testing n Introduction n Mocking to Spock
  • 4. Spock Testing Spock Intro 4
  • 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. Spock Can... n Reduces n Make n Be Spock Testing lines of code tests more readable extended 6
  • 7. Spock Testing JUnit 7
  • 8. Taxonomy of JUnit Spock Testing
  • 9. Spock Testing JUnit Pain Points? 9
  • 10. Unit testing needs Spock Testing n Concise, Clear and Readable n Promote “user” thinking ¨context ¨stimulus ¨expectations n Productivity 10
  • 11. Spock Testing Groovy 11
  • 12. Taxonomy of GroovyTestCase Spock Testing
  • 13. Groovy and Testing Spock Testing
  • 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. Groovy Improvements Spock Testing n Concise, Clear and Readable n Promote “user” thinking ¨context ¨stimulus ¨expectations n Productivity 15
  • 16. Spock Testing Spock 16
  • 17. Spock Spock Testing n Programmers Environment ¨Groovy n Promotes Clarity ¨structural blocks ¨removes noise 17
  • 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. Spock Testing Taxonomy of a Spec 19
  • 20. First Spec Spock Testing
  • 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. More Fields Spock Testing n Shared ¨Setup once ¨think setupSpec() n statics ¨only use for constants 22
  • 23. Fixture Methods Spock Testing n before / after a feature n before / after a spec n optional 23
  • 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. 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. Common Blocks Spock Testing n When / Then / Where n Given / When / Then 26
  • 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. 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. Conditions n checking Spock Testing for exceptions 29
  • 30. Throwing an Exception NOT! Spock Testing 30
  • 31. Mocks and Expectations Spock Testing 31
  • 32. Cleanup on Feature X n cleanup Spock Testing block ¨only followed by a where block ¨no repeats 32
  • 33. Where Blocks Spock Testing ¨last in a method ¨no repeats ¨used for data-driven features 33
  • 34. Helper Methods ¨either Spock Testing return a boolean n or ¨assert 34
  • 35. Spock Testing Specification Functions and Spock.lang.* 35
  • 36. Spock Testing n old() n thrown() n with / notThrown {} 36
  • 37. Spock.lang.* Documentation Spock Testing @Title @Narrative @Issue @See @Subject 37
  • 38. Spock.lang.* Ignoring Stuff Spock Testing @Requires @IgnoreIf @Ignore @IgnoreRest 38
  • 39. Reporting Spock Testing @Unroll 39
  • 40. Built-in Extensions: AutoCleanup Spock Testing @AutoCleanup @AutoCleanup(‘dispose’) @AutoCleanup(quite=true) 40
  • 41. Built-in Extensions: Timeout Spock Testing @Timeout @Timeout(10) @Timeout(value=10, unit=TimeUnit.MILLISECONDS) 41
  • 42. Built-in Extensions: Stepwise Spock Testing @Stepwise 42
  • 43. Spock Testing Hamcrest 43
  • 44. Spock Testing JUnit Rules 44
  • 45. Spock Testing SpockConfig 45
  • 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. 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