testing legacy apps with spock

12
Marcin Gryszko @mgryszko TESTING LEGACY APPS WITH SPOCK

Upload: marcin-gryszko

Post on 01-Dec-2014

2.373 views

Category:

Technology


1 download

DESCRIPTION

Presentation for the 1. meeting of Madrid Groovy User GroupTwitter:http://twitter.com/madridgugGoogle group:https://groups.google.com/group/madrid-gug

TRANSCRIPT

Page 1: Testing legacy apps with Spock

Marcin Gryszko

@mgryszko

TESTING LEGACY APPS WITH SPOCK

Page 2: Testing legacy apps with Spock

MY MOTIVATIONRetrofit a legacy app (Java + Hibernate + Spring) with automated tests

Why not with Groovy?

But what tool?

Page 3: Testing legacy apps with Spock

THE ANSWER IS...

Spock!

Page 4: Testing legacy apps with Spock

WHY GROOVY AND SPOCK FOR A JAVA DEVELOPER?

Groovy• powerful syntax

• code less and do more

Spock• BDD style testing

• describe features through executable specifications

• documentation and examples of legacy code

• Integrates with Spring & DbUnit

• Runs with JUnit 4 => Maven, CI

Page 5: Testing legacy apps with Spock

Extend spock.lang.Specification

class BankDaoSpec extends Specification

Define a feature (test) method

def “finds a bank by id"() {

// blocks for 4 test phases

setup:

when:

then:

}

HOW DO I TEST?

Page 6: Testing legacy apps with Spock

FEATURE METHOD BLOCKS

4 test phases in blocks

• Setup => setup: / given: / where:

• Execute => when: / expect:

• Verify => then: / expect:

• Teardown => cleanup:

• and:

Page 7: Testing legacy apps with Spock

DATA-DRIVEN TESTS

With where block

where:

bankName << ['BPH', 'BOS', 'HSBC']

Page 8: Testing legacy apps with Spock

MODULES

Spock Spring

• Annotate spec with @ContextConfiguration

• Use Spring DI (@Autowired, @Resource)

• Almost all TestContext annotations work

Spock Unitils

• Unitils – library of test helpers (mocking, persistence, Spring, …)

• Wrapper for DbUnit

• Spring integration – was useful when Spring TestContext was not yet implemented

• Not actively developed

Page 9: Testing legacy apps with Spock

MORE INTEGRATIONS

Spock Grails & Grails plugin

http://www.grails.org/plugin/spock

Geb integration (GebSpec)http://geb.codehaus.org/manual/latest/integrations.html#spock

Guice, Maven, Tapestry

Page 10: Testing legacy apps with Spock

Enough talk, let’s code!And implement a specification

https://github.com/mgryszko/blog-spock-spring-unitils

Page 11: Testing legacy apps with Spock

RESOURCES

Spock

http://code.google.com/p/spock/

Groovier testing with Spock

http://www.slideshare.net/rfletcher/groovier-testing-with-spock

Testing A Legacy Java Application With Groovy, Spock, Spring Test And Unitils

http://mgryszko.wordpress.com/2011/02/15/testing-a-legacy-java-application-with-groovy-spock-spring-test-and-unitils/

Unitils

http://www.unitils.org/

Page 12: Testing legacy apps with Spock

THANK YOU!