Top Banner
Is Groovy Better For Testing Than Java? Trisha Gee (@trisha_gee) Developer Advocate, JetBrains
55
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Is Groovy better for testing than Java?

Is Groovy Better For Testing Than Java?

Trisha Gee (@trisha_gee)

Developer Advocate, JetBrains

Page 2: Is Groovy better for testing than Java?
Page 3: Is Groovy better for testing than Java?

TDD Test Driven Development Design

Page 4: Is Groovy better for testing than Java?

Acceptance, Integration, System, Performance, Unit Tests

Page 5: Is Groovy better for testing than Java?

Shared understanding & pair programming

Page 6: Is Groovy better for testing than Java?
Page 7: Is Groovy better for testing than Java?

Open source

Page 8: Is Groovy better for testing than Java?

Downloadable library

Page 9: Is Groovy better for testing than Java?

The most used driver

Page 10: Is Groovy better for testing than Java?

The oldest driver

Page 11: Is Groovy better for testing than Java?

About to be re-written

Page 12: Is Groovy better for testing than Java?

But the tests were…

Page 13: Is Groovy better for testing than Java?

Ugly

Page 14: Is Groovy better for testing than Java?

Inconsistent

Page 15: Is Groovy better for testing than Java?

Hard to understand

Page 16: Is Groovy better for testing than Java?

“Test second”

Page 17: Is Groovy better for testing than Java?

Tightly tied to implementation

Page 18: Is Groovy better for testing than Java?

Functional / integration

Page 19: Is Groovy better for testing than Java?

Testing more than one thing

Page 20: Is Groovy better for testing than Java?

Mostly “happy path”

Page 21: Is Groovy better for testing than Java?

Not helpful for understanding the system’s expected behaviour

Page 22: Is Groovy better for testing than Java?

On the plus side…

Page 23: Is Groovy better for testing than Java?

The project does have tests

Page 24: Is Groovy better for testing than Java?

…but no coherent approach to testing

Page 25: Is Groovy better for testing than Java?

The tests should:

Page 26: Is Groovy better for testing than Java?

Be readable

Page 27: Is Groovy better for testing than Java?

Be easy to write

Page 28: Is Groovy better for testing than Java?

Describe the expected behaviour of the system

Page 29: Is Groovy better for testing than Java?

Possible solutions

•EasyMock / Mockito / JMock

•Home-grown mocking/stubbing

•Standards / Examples

•DSL – Domain Specific Language

•Hamcrest matchers

•Spock

Page 30: Is Groovy better for testing than Java?

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?

Page 31: Is Groovy better for testing than Java?

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'

Page 32: Is Groovy better for testing than Java?

The old tests were ugly

•MongoClientURITest

• -> MongoClientURISpecification

Page 33: Is Groovy better for testing than Java?

Mocking should be easy

• IterableCodecTest

• -> IterableCodecSpecification

Page 34: Is Groovy better for testing than Java?

Stubbing should also be easy

•DBCursorFunctionalSpecification• 'should use provided decoder factory‘

Page 35: Is Groovy better for testing than Java?

Hamcrest makes testing easier

•DBCollectionFunctionalSpecification• 'should return a list of all the values of a given field

without duplicates‘

Page 36: Is Groovy better for testing than Java?

The gains are not always that big

•DBCursorOldTest.testTailableImplicitAwaitOnHasNextOriginal

• ->DBCursorFunctionalSpecification• 'should block and wait for new documents when cursor is

tailable'

Page 37: Is Groovy better for testing than Java?

Answers

• Is it fast enough to learn that a week can give noticeable progress?• Yes.• And if in doubt, write Java

Page 38: Is Groovy better for testing than 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

Page 39: Is Groovy better for testing than Java?

Answers

•What benefits does it give us over our current tests?• Readability, structure, mocking, stubbing, data driven

testing

Page 40: Is Groovy better for testing than Java?

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

Page 41: Is Groovy better for testing than Java?

Answers

•Will it help developers write “better” tests?• Yes

Page 42: Is Groovy better for testing than Java?

Answers

•Does is make the tests more understandable?• Yes

Page 43: Is Groovy better for testing than Java?

So which is best, Groovy or Java?

Page 44: Is Groovy better for testing than Java?

Lines of Code

Page 45: Is Groovy better for testing than Java?

Ease of Understanding

Page 46: Is Groovy better for testing than Java?

Ease of Writing

Page 47: Is Groovy better for testing than Java?

Performance

Page 48: Is Groovy better for testing than Java?

Fast Feedback

Page 49: Is Groovy better for testing than Java?

IDE Support

Page 50: Is Groovy better for testing than Java?

Ease of Learning (for Java devs)

Page 51: Is Groovy better for testing than Java?

Ease of Learning (for other devs)

Page 52: Is Groovy better for testing than Java?

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

Page 53: Is Groovy better for testing than 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

Page 54: Is Groovy better for testing than Java?

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

Page 55: Is Groovy better for testing than Java?

http://bit.ly/GroovyVsJava

Resources

@trisha_gee