Your SlideShare is downloading. ×
Spock: Logical Testing for Enterprise Applications
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Spock: Logical Testing for Enterprise Applications

493
views

Published on

Speaker: Kenneth Kousen …

Speaker: Kenneth Kousen
The Spock framework brings simple, elegant testing to Java and Groovy projects. It integrates cleanly with JUnit, so Spock tests can be integrated as part of an existing test suite. Spock also includes an embedded mocking framework that can be used right away.
In this presentation, we'll look at several examples of Spock tests and review most of its capabilities, including mock objects and integration with Spring.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
493
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
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. Spock I have been, and always shall be, your friendly testing framework
  • 2. Who Am I? Ken Kousen Kousen IT, Inc. http://www.kousenit.com ken.kousen@kousenit.com @kenkousen Making Java Groovy http://manning.com/kousen
  • 3. What is Spock? Testing framework Written in Groovy A logical framework for enterprise testing Combination of Specification + Mock
  • 4. The Search for Spock Spock home page http://spockframework.org redirects to http://code.google.com/p/spock Github https://github.com/spockframework/spock
  • 5. Create a Spock test Extend spock.lang.Specification class MySpec extends Specification
  • 6. Simple Specification Demo: Palindrome Checker
  • 7. Fixture Methods def setup() {} run before every feature method def cleanup() {} run after every feature method def setupSpec() {} run before first feature method def cleanupSpec() {} run after last feature method Like JUnit 4: @Before, @After, @BeforeClass, @AfterClass
  • 8. Feature Methods Test methods def "descriptive name"() { // blocks }
  • 9. Blocks setup: given: cleanup: when: Stimulus then: Response, booleans are checked expect: where:
  • 10. when: and then: when: Contents are arbitrary then: conditions exceptions interactions (mocks described below) Always occur together
  • 11. what they thought old Kirk would look like old Method Sweet method in Specification class expression value before where block when: obj.count() then: count == old(count) + 1 what he actually looks like
  • 12. @Shared Annotation for shared objects Note: instance fields are not shared @Shared res = new VeryExpensiveResource() @Shared sql = Sql.newInstance(...)
  • 13. @Shared Demo: Library Computer
  • 14. Exceptions are exceptions evil or just goatees? thrown() and notThrown() then: thrown(SqlException) -- or -SqlException e = thrown() e.sqlCode == ... Can do work after catching exception
  • 15. Parameterized feature methods Tests that iterate through data Use where: clause expect: name.size() == length where: [name,length] << [['Kirk',4],['Spock',5]]
  • 16. Data Table where: clause supports data tables expect: where: name 'Kirk' 'Spock' 'McCoy' name.size() == length | | | | length 4 5 5 Shouldn't Data run on Android?
  • 17. where: clause Supports anything Groovy can iterate over expect: x + y == z where: [x,y,z] << sql.rows('select x,y,z from ...')
  • 18. @Unroll Display separate message for each row of data Spock 0.5: @Unroll({"...$var..."}) Spock 0.6+: @Unroll def "my test #var ..."() { ... }
  • 19. Data Specs Demos: Hello, Spock! DataDriven DatabaseDriven StadiumLocationsSpec
  • 20. Interactions Working with Mock objects interaction NO KILL I
  • 21. Creating Mocks Two syntax options: def items = Mock(List) List items = Mock() Can mock interfaces with standard libs Mock classes with CGLIB
  • 22. Setting Expectations Use >> operator list.get(0) >> 'data'
  • 23. Global vs Local Global: Defined outside then: block Valid to end of feature method Local: Defined inside then: block Valid inside preceding when: block
  • 24. Optional No cardinality Must have return value then: list.get(0) >> 'data'
  • 25. Required Must have cardinality May have return value then: 1 * list.get(0) >> 'data' then: 3 * list.size()
  • 26. Cardinalities Ranges with wildcard Wildcard is _ (underscore) 3 * list.size() (3.._) * list.size() (_..3) * list.size() // 3 times // 3 or more // up to 3
  • 27. All sorts of constraints Regex Any set method with one arg pojo./set.*/(_) Nulls, not null pojo.method(!null)
  • 28. All sorts of constraints as Operator dir.listFiles(_ as FileFilter) Closures count.increment { it ==~ /a*b/ }
  • 29. Testing Invocation Order Use multiple then blocks def 'testing order of methods'() { when: obj.method() then: 1*collaborator.method1() then: 1*collaborator.method2() }
  • 30. Interactions Demos: PublisherSubscriber TribbleSpec
  • 31. Extensions @Timeout @Ignore @IgnoreRest @FailsWith
  • 32. @FailsWith Test fails with expected exception @FailsWith(TooFewInvocationsError) def "required interaction"() { def m = Mock(List) 2 * m.get(_) >> "foo" expect: m.get(3) == "foo" }
  • 33. @FailsWith Parameters value=ExpectedExceptionOrError reason="reason for failure"
  • 34. @Ignore and @IgnoreRest @Ignore Don't run a particular test or test class Optional value = reason @IgnoreRest Don't run any OTHER tests
  • 35. interaction block Method in Specification class Takes closure argument def 'use interaction block'() { when: obj.method() then: interaction { // do whatever } }
  • 36. BDD Behavior Driven Development Each block accepts string description Just documentation def "number of tribbles in storage compartment"() { given: "average litter of 10" and: "new generation every 12 hours over a period of three days" when: "tribbles get into storage compartments" then: "compute number of tribbles" }
  • 37. Spock's Own Tests Like most modern open source projects Documentation can be thin or outdated Tests are excellent See "smoke" tests in source
  • 38. Links http://github.com/spockframework/spock http://code.google.com/p/spock http://spockframework.org http://meet.spockframework.org http://groups.google.com/group/spockframework?pli=1 Source code for examples is from 1. spock-example project https://github.com/spockframework/spock-example 2. my GitHub repo https://github.com/kousen/Spock-NFJS-Article
  • 39. Session Evals Please complete your session evals