IT Modernization Doesn’t Mean You Leave Your Legacy Apps Behind
Testing legacy apps with Spock
-
Upload
marcin-gryszko -
Category
Technology
-
view
2.372 -
download
1
description
Transcript of Testing legacy apps with Spock
![Page 1: Testing legacy apps with Spock](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/1.jpg)
Marcin Gryszko
@mgryszko
TESTING LEGACY APPS WITH SPOCK
![Page 2: Testing legacy apps with Spock](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/3.jpg)
THE ANSWER IS...
Spock!
![Page 4: Testing legacy apps with Spock](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/7.jpg)
DATA-DRIVEN TESTS
With where block
where:
bankName << ['BPH', 'BOS', 'HSBC']
![Page 8: Testing legacy apps with Spock](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/10.jpg)
Enough talk, let’s code!And implement a specification
https://github.com/mgryszko/blog-spock-spring-unitils
![Page 11: Testing legacy apps with Spock](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061203/547ce293b479597f508b479e/html5/thumbnails/12.jpg)
THANK YOU!