is groovy better for testing than java?
TRANSCRIPT
Possible solutions
•EasyMock / Mockito / JMock
•Home-grown mocking/stubbing
•Standards / Examples
•DSL – Domain Specific Language
•Hamcrest matchers
•Spock
A week to spike Spock
•Questions to answer:• Is it fast enough to learn that a week can give noticeable
progress?• Is using Groovy to test Java too big a context switch?• What benefits does it give us over our current tests?• What benefits does it give us over the alternatives?• Will it help developers write “better” tests?• Does is make the tests more understandable?
Intro to Spock & Groovy
•DBCollectionFunctionalSpecification• 'should update multiple documents‘• 'drop index should error if index does not exist‘
• JMXConnectionPoolListenerSpecification• 'should create a valid ObjectName for ipv4 addresses'
Stubbing should also be easy
•DBCursorFunctionalSpecification• 'should use provided decoder factory‘
Hamcrest makes testing easier
•DBCollectionFunctionalSpecification• 'should return a list of all the values of a given field
without duplicates‘
The gains are not always that big
•DBCursorOldTest.testTailableImplicitAwaitOnHasNextOriginal
• ->DBCursorFunctionalSpecification• 'should block and wait for new documents when cursor is
tailable'
Answers
• Is it fast enough to learn that a week can give noticeable progress?• Yes.• And if in doubt, write Java
Answers
• Is using Groovy to test Java too big a context switch?• No. Although you’ll want the new Map syntax in prod
code
Answers
•What benefits does it give us over our current tests?• Readability, structure, mocking, stubbing, data driven
testing
Answers
•What benefits does it give us over the alternatives?• Mocking and stubbing are very easy and very readable• A DSL or DSL-style could be introduced via Groovy• You can still make use of Hamcrest Matchers
Other Advantages
•Shows how the library can be used from Groovy
•Allows us to learn a new language without impacting production
•Can just write Java
Other Disadvantages
•Requires investment in learning to get full advantages
•Some context switching
•We now have (at least) two different ways to test our code
•…and a possible migration task
Conclusion
•Groovy & Spock are:• Easy to read and understand• Easy to write• Easy to learn
• Java and JUnit are:• Well understood standards• Type safe and performant• Better supported by IDEs