testing legacy apps with spock
DESCRIPTION
Presentation for the 1. meeting of Madrid Groovy User GroupTwitter:http://twitter.com/madridgugGoogle group:https://groups.google.com/group/madrid-gugTRANSCRIPT
Marcin Gryszko
@mgryszko
TESTING LEGACY APPS WITH SPOCK
MY MOTIVATIONRetrofit a legacy app (Java + Hibernate + Spring) with automated tests
Why not with Groovy?
But what tool?
THE ANSWER IS...
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
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?
FEATURE METHOD BLOCKS
4 test phases in blocks
• Setup => setup: / given: / where:
• Execute => when: / expect:
• Verify => then: / expect:
• Teardown => cleanup:
• and:
DATA-DRIVEN TESTS
With where block
where:
bankName << ['BPH', 'BOS', 'HSBC']
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
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
Enough talk, let’s code!And implement a specification
https://github.com/mgryszko/blog-spock-spring-unitils
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/
THANK YOU!