is groovy better for testing than java?

55
Is Groovy Better For Testing Than Java? Trisha Gee (@trisha_gee) Developer Advocate, JetBrains

Upload: trisha-gee

Post on 18-Jul-2015

2.048 views

Category:

Technology


3 download

TRANSCRIPT

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.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

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