KEMBAR78
Is Groovy better for testing than Java? | PPTX
Is Groovy Better For
Testing Than Java?
Trisha Gee (@trisha_gee)
Developer Advocate, JetBrains
TDD
Test Driven Development Design
Acceptance, Integration, System,
Performance, Unit Tests
Shared understanding & pair
programming
Open source
Downloadable library
The most used driver
The oldest driver
About to be re-written
But the tests were…
Ugly
Inconsistent
Hard to understand
“Test second”
Tightly tied to implementation
Functional / integration
Testing more than one thing
Mostly “happy path”
Not helpful for understanding
the system’s expected behaviour
On the plus side…
The project does have tests
…but no coherent approach to
testing
The tests should:
Be readable
Be easy to write
Describe the expected behaviour
of the system
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'
The old tests were ugly
•MongoClientURITest
•-> MongoClientURISpecification
Mocking should be easy
•IterableCodecTest
•-> IterableCodecSpecification
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.testTailableImplicitAwaitOnHas
NextOriginal
•->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
Answers
•Will it help developers write “better” tests?
• Yes
Answers
•Does is make the tests more understandable?
• Yes
So which is best, Groovy or
Java?
Lines of Code
Ease of Understanding
Ease of Writing
Performance
Fast Feedback
IDE Support
Ease of Learning (for Java devs)
Ease of Learning (for other devs)
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
http://bit.ly/GroovyVsJava
Resources
@trisha_gee

Is Groovy better for testing than Java?

Editor's Notes

  • #2 Who’s using IntelliJ IDEA? …for groovy? Who’s using Groovy? Who’s heard of Spock? Who’s using Spock?
  • #3 Mentioned in this morning’s talk about reactive frameworks
  • #5 Had a CI environment for running the full suite in 40 minutes Failed Fast
  • #17 “Test second” Tightly tied to implementation Functional/integration tests Often testing more than one thing Mostly “happy path”
  • #19 Functional/integration tests Often testing more than one thing Mostly “happy path”
  • #24 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
  • #25 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
  • #27 And easy to understand
  • #28 So developers write them! Preferably writing GOOD tests
  • #42 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
  • #43 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
  • #45 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
  • #46 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
  • #47 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
  • #48 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
  • #49 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
  • #50 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
  • #51 Advantages of Java: More familiar for Java developers to write and read
  • #52 Advantages of Java: More familiar for Java developers to write and read
  • #55 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