nfjs software symposium series 2015 - jfokus java... · nfjs software symposium series 2015 ken...

47
NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock

Upload: others

Post on 25-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

NFJS Software Symposium Series 2015

Ken Sipe

Advanced Java Unit Testing with Spock

Page 2: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingAbout Speaker

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

http://kensipe.blogspot.com/ http://del.icio.us/kensipe twitter: @kensipe [email protected]

Page 3: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingAgenda

nTesting nIntroduction to Spock nMocking

Page 4: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Spock Intro

4

Page 5: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

ntesting framework...

nbased on Groovy

nfully compatible with JUnit

nresult of learnings from ¨RSpec, BDD, JUnit

5

Spock is...

Page 6: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingSpock Can...

nReduces lines of code

nMake tests more readable

nBe extended

6

Page 7: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

JUnit

7

Page 8: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingTaxonomy of JUnit

Page 9: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

9

JUnit Pain Points?

Page 10: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingUnit testing needs

nConcise, Clear and Readable nPromote “user” thinking

¨context ¨stimulus ¨expectations

nProductivity

10

Page 11: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Groovy

11

Page 12: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingTaxonomy of GroovyTestCase

Page 13: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingGroovy and Testing

Page 14: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingGroovy Power Assert

Condition not satisfied:

interest == calc.calculate(amt, year) | | | | | |

25.0 | | 20.0 100 4 | com.math.SimpleInterestCalculator@606e1dec

false

Page 15: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingGroovy Improvements

nConcise, Clear and Readable nPromote “user” thinking

¨context ¨stimulus ¨expectations

nProductivity

15

Page 16: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Spock

16

Page 17: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingSpock

nProgrammers Environment ¨Groovy

nPromotes Clarity ¨structural blocks ¨removes noise

17

Page 18: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingWhy use Spock?

nExpressive testing language nEasy to learn nUsable from unit to end-to-end nLeverages Groovy nRuns with JUnit Runner

¨IDE ¨CI

18

Page 19: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Taxonomy of a Spec

19

Page 20: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingFirst Spec

Page 21: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingTerms

nSpecification ¨compare to TestCase or GroovyTestCase ¨Instructs JUnit to run with Sputnik (JUnit runner)

nFields ¨initialized for each “test” ¨think “setup” ¨not shared between feature methods

21

Page 22: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingMore Fields

nShared ¨Setup once ¨think setupSpec()

nstatics ¨only use for constants

22

Page 23: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingFixture Methods

nbefore / after a feature nbefore / after a spec noptional

23

Page 24: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingFeature Methods

n“heart” of spec nfour phases

¨setup the features fixture ¨provide stimulus to system ¨describes the response ¨clean up

24

Page 25: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingBlocks

25

given: preconditions, data fixtures

when: actions that trigger some outcomethen: 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

Page 26: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingCommon Blocks

nWhen / Then / Where nGiven / When / Then

26

Page 27: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingBlocks

nsetup

¨must be first ¨must be the only ¨no special semantics ¨label is optional ¨label given: is an alias

27

Page 28: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingWhen / Then Blocks

nused together ¨possible to have many per feature

nthen restrictions ¨conditions ¨exception conditions ¨automatic asserts ¨interactions ¨variable defs

28

Page 29: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingConditions

nchecking for exceptions

29

Page 30: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingThrowing an Exception NOT!

30

Page 31: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingMocks and Expectations

31

Page 32: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingCleanup on Feature X

ncleanup block ¨only followed by a where block ¨no repeats

32

Page 33: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingWhere Blocks

¨last in a method ¨no repeats ¨used for data-driven features

33

Page 34: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingHelper Methods

¨either return a boolean nor

¨assert34

Page 35: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Specification Functionsand Spock.lang.*

35

Page 36: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

nold() nthrown() / notThrown nwith {}

36

Page 37: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingSpock.lang.* Documentation

37

@Title @Narrative @Issue @See @Subject

Page 38: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingSpock.lang.* Ignoring Stuff

38

@Requires @IgnoreIf @Ignore @IgnoreRest

Page 39: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingReporting

39

@Unroll

Page 40: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingBuilt-in Extensions: AutoCleanup

40

@AutoCleanup @AutoCleanup(‘dispose’) @AutoCleanup(quite=true)

Page 41: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingBuilt-in Extensions: Timeout

41

@Timeout @Timeout(10) @Timeout(value=10, unit=TimeUnit.MILLISECONDS)

Page 42: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingBuilt-in Extensions: Stepwise

42

@Stepwise

Page 43: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

Hamcrest

43

Page 44: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

JUnit Rules

44

Page 45: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

SpockConfig

45

Page 46: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock TestingReferences

46

nGetting Spock ¨http://code.google.com/p/spock/

nSource from Presentation ¨https://github.com/kensipe/spock-demos-nfjs

Page 47: NFJS Software Symposium Series 2015 - Jfokus Java... · NFJS Software Symposium Series 2015 Ken Sipe Advanced Java Unit Testing with Spock. About Speaker Spock Testing Developer:

Spock Testing

n Closing and Q&A

¨Please fill out the session evaluation ¨Ken Sipe

n [email protected] n kensipe.blogspot.com n twitter: @kensipe

Summary