Wednesday, April 21, 2010

Spock Framework Mock Objects: Better Groovy Based Testing

I wrote an article on the Canoo blog called "Spock and Test Spies: A Logical Choice".

If you are interested in alternative (and better) mock object frameworks in Groovy then check it out. Spock is a heavy user of Groovy AST Transformations, so those interested in transformations may also want to take a peek.

And if you have come here simply to vote the article up on DZone, then click away!


Krishna said...

Have been using gmock for writing against large legacy java code base for the past 2 weeks. I am certainly enjoying the benefits of mocking/testing using groovy as well pairing with peers to show off groovy's terseness. We have introduced groovy to the peers thru testing.
As tests are written against java code i cannot use categories, expando metaclass features of groovy.
i could leverage the following features of gmock in my tests static calls,partial mock,matchers (match + hamcrest). I do find intellij does not support auto completion for gmock methods. when i use methods like chain() i get method not found exception during runtime.
I see spock trying to use BDD idioms (given(setup)/when/then) and terseness in matchers.
i am curious to find the comparison chart between gmock and spock.

Hamlet D'Arcy said...

Yes, testing Java code means many of the dynamic aspects of Groovy simply don't work once you enter Java code.

Spock is statically typed and occurs at the Compiler, and does not depend on many of the dynamic features. It might be a better fit. Also, IDEA should play better with Spock because the objects in your test have real types... whereas gmock uses a lot of dynamic dispatch.

I have not had to test Java with Spock yet, but I think it sounds like a better approach than GMock. Let me know what you find.

Peter Niederwieser said...

The primary difference between Spock and GMock is that Spock is a "full-stack" developer testing framework which happens to include a mocking framework, whereas GMock is "just" a mocking framework (not that this is a bad thing). Spock's mocking framework is quite powerful, and all its features work both for testing Java and Groovy code (this is a design principle of Spock). On the other hand, we aren't big fans of dark mocking magic, and hence Spock doesn't (and maybe never will) support features like mocking static methods. In those cases where you really need such features, you could always use GMock (or PowerMock, etc.) together with Spock.

Jagna Co Kalani said...

Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.