• No results found

Smarter Testing With Spock

N/A
N/A
Protected

Academic year: 2021

Share "Smarter Testing With Spock"

Copied!
34
0
0

Loading.... (view fulltext now)

Full text

(1)

Smarter

Testing

With Spock

P e t e r N i e d e r w i e s e r

(2)

What we’ll talk

about

Spock?!

State Based Testing Data Driven Testing

Interaction Based Testing Spock Extensions

(3)
(4)

Spock is...

A developer testing framework...

for Groovy and Java applications... based on Groovy...

fully compatible with JUnit... but going beyond!

(5)

Spock Can...

Reduce the lines of test code Make tests more readable

Turn tests into specifications Be extended in powerful ways Bring back the fun to testing!

(6)

Getting Started

Homepage

http://spockframework.org Source Code

https://github.com/spockframework/spock Spock Web Console

http://meet.spockframework.org Spock Example Project

http://downloads.spockframework.org

https://github.com/spockframework/spock/tree/groovy-1.8/ spock-example

Slides and Code for this Presentation

(7)

Who’s Using Spock?

Geb Gradle GPars

Apache Tapestry Griffon

Grails Plugin Collective

Spock Grails

(8)

Who’s Using Spock? (2)

Energized Work SystemsForge IntelliGrape Software CTI Digital Software Projects eHarmony be2 Gennemtænkt IT Smarter Ecommerce BSkyB bemoko

(9)

State Based

Testing

(10)

Classical Unit Testing Arrange

Act

Assert

Given-When-Then

(11)

Classical Unit Testing Arrange

Act

Assert

Given-When-Then

State Based Testing

Sho

w m

e th

e c

(12)

Recap: State Based

Testing

Blocks

setup: cleanup: expect: given: when: then: where: and:

Fixture Methods

setup() cleanup() setupSpec() cleanupSpec() Instance and @Shared fields

(13)

Data Driven

Testing

(14)

Data Driven Testing

Test the same behavior... with varying data!

(15)

Data Driven Testing

Test the same behavior... with varying data!

Show

me

the

code

(16)

Recap: Data Driven

Testing

where: block Data tables

External data sources @Unroll

(17)

Interaction

Based Testing

(18)

Interaction Based

Testing

Design and test how your objects communicate

Mocking frameworks to the rescue Spock comes with its own mocking framework

(19)

Interaction Based

Testing

Design and test how your objects communicate

Mocking frameworks to the rescue Spock comes with its own mocking framework

Show

me

the

(20)

Recap: Interaction

Based Testing

Creating

Mocking

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")})

(21)

Recap: Interaction

Based Testing (2)

Stubbing

Mocking and Stubbing

Impressing your friends

// now returns status code

String receive(String msg) { ... }

 

sub.receive(_) >> "ok"

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

sub.receive(_) >>> { msg -> msg.size() > 3 ? "ok" :

"fail" }

(22)

Spock

(23)

Spock Extensions

Listeners

Interceptors

Annotation-driven extensions Global extensions

(24)

Built-in Extensions

@Ignore @IgnoreRest @FailsWith @Timeout @AutoCleanup @Stepwise @RevertMetaClass @Rule

(25)

Built-in Extensions

@Ignore @IgnoreRest @FailsWith @Timeout @AutoCleanup @Stepwise @RevertMetaClass

Show

me

the

code

!

(26)

External Extensions

spock-grails spock-spring spock-guice spock-tapestry spock-unitils spock-griffon spock-arquillian spock-extensions

(27)

Grails Extension

http://grails.org/plugin/spock

https://github.com/spockframework/spock-grails

grails install plugin spock 0.6 grails test-app

(28)

Grails Extension (2)

@TestFor(PersonController) @Mock(Person)

class PersonControllerSpec extends

Specification { 

    def "domain mocking"() {

        setup:  

        mockDomain(Person)

 

        when:

        new Person(name: name).save()

 

        then:

        Person.findByName(name) != null

 

        where:

        name = "bill"

    } }

(29)

Spring Extension

class InjectionExamples extends Specification {

    @Autowired     IService1 byType       @Resource     IService1 byName       @Autowired     ApplicationContext context }  

(30)

Other Cool

Stuff

(31)

Configuring Spock

runner {     filterStackTrace false     include Fast     exclude Slow     optimizeRunOrder true } @Fast

class MyFastSpec extends Specification {

    def "I’m fast as hell!"() { expect: true }

 

    @Slow

~/.spock/SpockConfig.groovy, or on class path, or with -Dspock.configuration

(32)

Tooling

Eclipse, IDEA

Ant, Maven, Gradle

Jenkins, Bamboo, TeamCity

Spock runs everywhere JUnit and Groovy run!

(33)

Spock Under The

Hood

You write... Spock generates... You see... a = 1; b = 2; c = 4 expect: sum(a, b) == c

def rec = new ValueRecorder()

verifier.expect(

    rec.record(rec.record(sum(rec.record(a),

        rec.record(b)) == rec.record(c)))

(34)

Q&A

Homepage

http://spockframework.org Source Code

https://github.com/spockframework/spock Spock Web Console

http://meet.spockframework.org Spock Example Project

http://downloads.spockframework.org

https://github.com/spockframework/spock/tree/groovy-1.8/ spock-example

Slides and Code for this Presentation

References

Related documents

To test the predictive accuracy of CopyRighter [ 7 ], PICRUSt [ 6 ], and PAPRICA [ 8 ] for genomes with known GCNs, we compared their predictions with the GCNs counted in

Mostly network layer protocol (i.e. LEACH routing protocol) suffers from many different attacks like; spoofing or altering the route information, selective

Martin Helmchen a réalisé de nombreux enregistrements ces dernières années parmi lesquels les Vingt regards sur l’Enfant Jésus d’Olivier Messiaen, les Varia - tions Diabelli

localized MOE and KAR using Eq. ( 6 ). Third, in manufac- turing process of GLT, the ends of same lamina grades are connected with finger joint. Thus, finger-jointed strengths

Ex- tremists are loyal when their party is in the opposition, but these same extremists become more likely to rebel when their party controls government.. Additionally, they

In Koyna an earthquake occurred in 1967 which surprised everybody since no such shock had struck the deccan plateau until then and this area was considered as stable from

Third, the indictment against Jesus was illegal because the judges themselves brought up an unprovable charge against Jesus without any prior testimony by witnesses.. Fourth,

We measured SCFA’s titer (butyrate, propionate and acetate) as an improvement parameter of women with functional constipation af- ter 21 days supplementation of fermented milk