The document discusses whether the Groovy programming language is better than Java for testing. It outlines some of the pros and cons of each approach. While Java and JUnit tests are performant and well-supported, Groovy and the Spock testing framework make tests more readable, easier to write, and help describe expected system behavior through features like mocking and hamcrest matchers. Migrating existing Java tests to Groovy would require learning a new language but allow exploring its testing advantages without impacting production code. In conclusion, both have tradeoffs and the best approach depends on the specific project needs and developer preferences.
29. Possible solutions
•EasyMock / Mockito / JMock
•Home-grown mocking/stubbing
•Standards / Examples
•DSL – Domain Specific Language
•Hamcrest matchers
•Spock
30. 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?
31. 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'
32. The old tests were ugly
•MongoClientURITest
•-> MongoClientURISpecification
33. Mocking should be easy
•IterableCodecTest
•-> IterableCodecSpecification
34. Stubbing should also be easy
•DBCursorFunctionalSpecification
• 'should use provided decoder factory‘
35. Hamcrest makes testing easier
•DBCollectionFunctionalSpecification
• 'should return a list of all the values of a given field
without duplicates‘
36. The gains are not always that big
•DBCursorOldTest.testTailableImplicitAwaitOnHas
NextOriginal
•->DBCursorFunctionalSpecification
• 'should block and wait for new documents when cursor is
tailable'
37. Answers
•Is it fast enough to learn that a week can give
noticeable progress?
• Yes.
• And if in doubt, write Java
38. Answers
•Is using Groovy to test Java too big a context
switch?
• No. Although you’ll want the new Map syntax in prod
code
39. Answers
•What benefits does it give us over our current
tests?
• Readability, structure, mocking, stubbing, data driven
testing
40. 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
52. 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
53. 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
54. 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
Who’s using IntelliJ IDEA?
…for groovy?
Who’s using Groovy?
Who’s heard of Spock?
Who’s using Spock?
Mentioned in this morning’s talk about reactive frameworks
Had a CI environment for running the full suite in 40 minutes
Failed Fast
“Test second”
Tightly tied to implementation
Functional/integration tests
Often testing more than one thing
Mostly “happy path”
Functional/integration tests
Often testing more than one thing
Mostly “happy path”
We do see the need of having tests
We encourage contributors to write tests for their code
We require tests for code review
We have a team of experienced Java developers
Have a coherent… well, any… approach to testing
Have any unit (i.e. fast) tests
Have examples of good tests
Know what the current tests really cover
And easy to understand
So developers write them!
Preferably writing GOOD tests
Given/when/then encourages developers to think about what they’re really testing
Easy mocking and stubbing means it’s much easier to write unit tests
Data driven testing makes it really easy to add edge cases, unhappy paths, what-ifs
Nice syntax for working with Maps
Pretty ways of creating Lists
String method names
Given/when/then gives clear structure
Groovy’s nice features for Strings
Using closures reduces boilerplate
Overall, even when nasty test-too-many-things tests are ported with a straight conversion, Spock’s conventions and Groovy’s boilerplate removal already make it more understandable
Groovy:
Reduced boilerplate, specifically for Strings, Maps, Lists and inner classes
Dynamic language means we only care about types when we really care
Advantages of Spock:
Mocking is easy and readable, and requires far fewer dependencies
Stubbing is similarly painless
Data Driven Testing is fantastic for testing many inputs vs output
Disadv:
Spock requires lines for given/when/then
Advantages of Java:
If we tested using Java 8 we get some reduced boilerplate
Disadvantages of Java
Boilerplate is better off hidden behind builders and utils
No lovely syntax for Collections
Creating Strings is ugly
Groovy:
Reduced boilerplate, specifically for Strings, Maps, Lists and inner classes
Dynamic language means we only care about types when we really care
Advantages of Spock:
Forced structure
Mocking is readable
Stubbing is similarly painless
Data Driven Testing is fantastic for testing many inputs vs output
Testing of Exceptions is much more comprehensive
Given/when/then helps developers understand what’s going on when reading
Strings as method names aids understanding
Advantages of Java:
Type safety: you can see what things are immediately
More familiar for Java developers to read
Disadvantages of Java
Too easy to clutter up tests with boilerplate
Too easy to write badly structured tests
No lovely syntax for Collections
Groovy:
Reduced boilerplate, specifically for Strings, Maps, Lists and inner classes
Dynamic language means we only care about types when we really care
Advantages of Spock:
Forced structure
Mocking is easy and requires far fewer dependencies
Stubbing is similarly painless (but takes a bit of practice)
Both of these means it’s easier to write fast unit tests
Data Driven Testing is fantastic for testing many inputs vs output
Testing of Exceptions is much more comprehensive
Given/when/then helps developers structure the code when writing…
Disadvantages of Spock/Groovy:
Learning Curve
Advantages of Java:
Type safety: failures can be caught before running
Type safety: refactoring is automagic
More familiar for Java developers to write
IDE support and autocomplete makes it easy to write stuff
Disadvantages of Java
No inherent structure to encourage good tests
Requires more discipline to write “better” tests
Unwieldy to work with collections
Creating Strings is ugly
Disadvantages of Groovy:
Slower to run individual tests
Less predictable (should be able to overcome with better tests)
Advantages of Java:
Faster (more importantly, more predictable) to run
Disadvantages of Java
If you’re not using Java 8, using some of the concurrency tools is verbose
Disadvantages of Groovy:
Slower to run individual tests
Slow feedback on simple errors (runtime instead of compile time)
Advantages of Spock:
Given/when/then gets you thinking up front about how valuable your tests are
Advantages of Java:
Type safety: failures can be caught before running
Groovy:
Improving support
Even dynamically typed objects have suggestions
Disadvantages of Groovy:
Refactoring is still not as good as Java
Harder to do Test Driven Development
Advantages of Spock:
Syntax highlighting
Advantages of Java:
Type safety: failures can be caught before running
Type safety: refactoring is automagic
Advantages of Java:
More familiar for Java developers to write and read
Advantages of Java:
More familiar for Java developers to write and read
Groovy and Spock make it really easy to encourage developers to write tests
And to write GOOD tests
Java and Junit are FAST, fast to run, fast to write, fast to get feedback
Well integrated with the IDE, better for writing tests first