spock

29
Spock Евгений Борисов [email protected]

Upload: evgeny-borisov

Post on 20-Nov-2014

464 views

Category:

Software


0 download

DESCRIPTION

JEConf 2014, Kiev

TRANSCRIPT

Page 1: Spock

SpockЕвгений Борисов

[email protected]

Page 2: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 3: Spock

3

The creator of SpockGroovy & Gradle committerWorks for Gradleware@pniederw

Peter Niederwieser

Page 4: Spock

A developer testing framework...for Groovy and Java applications...based on Groovy...fully compatible with JUnit...but going beyond!

Spock is...

Page 5: Spock

Homepagehttp://spockframework.org

Source Codehttp://github.spockframework.org/spock

Documentationhttp://docs.spockframework.org/en/latest

Spock Web Consolehttp://webconsole.spockframework.org

Spock Example Projecthttp://github.spockframework.org/spock-example

Slides and Code for this Presentationhttp://github.spockframework.org/smarter-testing-with-spock

Getting Started

Page 6: Spock

Who’s Using Spock?

Page 7: Spock

Who’s Using Spock? (2)

Page 8: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 9: Spock

Classical Unit TestingArrangeActAssert

Given-When-Then

State Based Testing

Page 10: Spock

Classical Unit TestingArrangeActAssert

Given-When-Then

State Based Testing

Show me th

e code!

Page 11: Spock

• Blockssetup: cleanup: expect: given: when: then: where: and:• Fixture Methods

setup() cleanup() setupSpec() cleanupSpec()• Instance and @Shared fields• old() and thrown()

• Forces developers to think like users• Nice looking reports (that no one reads)

Recap: State Based Testing

Page 12: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 13: Spock

Test the same behavior......with varying data!

Data Driven Testing

Page 14: Spock

Test the same behavior......with varying data!

Data Driven Testing

Show me the code!

Page 15: Spock

where: blockData tablesExternal data sources@Unroll

Recap: Data Driven Testing

Page 16: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 17: Spock

How objects communicateMocking frameworks simplify the jobSpock has its own mocking framework

Interaction Based Testing

Page 18: Spock

How objects communicateMocking frameworks simplify the jobSpock has its own mocking framework

Google it: “Mocks aren't Stubs”

Interaction Based Testing

Page 19: Spock

How objects communicateMocking frameworks simplify the jobSpock has its own mocking framework

Interaction Based Testing

Show me the code!

Page 20: Spock

Creating

Mocking

Recap: Interaction Based Testing

def sub = Mock(Subscriber)Subscriber sub = Mock()

1 * sub.receive("msg")(1..3) * sub.receive(_)(1.._) * sub.receive(_ as String)1 * sub.receive(!null)1 * sub.receive({it.contains("m")})1 * _./rec.*/("msg")

Page 21: Spock

Stubbing

Mocking and Stubbing

Impressing your friends

Recap: Interaction Based Testing (2)

// now returns status codeString receive(String msg) { ... }  sub.receive(_) >> "ok"sub.receive(_) >>> ["ok", "ok", "fail"]sub.receive(_) >> { msg -> msg.size() > 3 ? "ok" : "fail" }

3 * sub.receive(_) >>> ["ok", "ok", "fail"]

(_.._) * _._(*_) >> _

Page 22: Spock

NEW IN 0.7What’s new?

Page 23: Spock

Group conditions/interactions w/ same target

Better mocking failure messages

New in 0.7 (1)

with(person) {     name == “Fred”   age == 30   sex == “male”} with(service) {   1 * start()   1 * stop()}

Page 24: Spock

Stubs

Spies

Groovy mocks

Declare interactions upfront

More at:http://docs.spockframework.org/en/latest/new_and_noteworthy.html

New in 0.7 (2)

def person = Stub(Person)

def person = Spy(Person, constructorArgs: ["Fred", 30])

def person = GroovyMock(person) // GroovyStub, GroovySpy

def person = Mock(Person) {    1 * sleep()    sing() >> “tra-la-la”}

Page 25: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 26: Spock

ListenersInterceptorsAnnotation-driven extensionsGlobal extensions

Spock Extensions

Page 27: Spock

Spring Extension

@ContextConfiguration(locations = ["my-app-context.xml"])class InjectionExamples extends Specification {         @Autowired    Service1 injectedByType      @Resource    Service1 injectedByName      @Autowired    ApplicationContext context}

Page 28: Spock

Meta & IntroState Based TestingData Driven TestingInteraction Based TestingSpock ExtensionsMore Cool Stuff

What we’ll talk about

Page 29: Spock

Homepagehttp://spockframework.orgSource Codehttp://github.spockframework.org/spockDocumentationhttp://docs.spockframework.org/en/latestSpock Web Consolehttp://webconsole.spockframework.orgSpock Example Projecthttp://github.spockframework.org/spock-exampleSlides and Code for this Presentationhttp://github.spockframework.org/smarter-testing-with-spockMocks Aren’t Stubshttp://martinfowler.com/articles/mocksArentStubs.html

Q&A