Tapestry Training -- From The Source

Let me help you get your team up to speed in Tapestry ... fast. Visit howardlewisship.com for details on training, mentoring and support!

Thursday, April 19, 2012

Yet Another Bit of Spock Love

I'm gradually converting a back-log of existing tests to Spock ... and some of them convert so beautifully, it hurts. Here's an example:

Before (Java and TestNG)

After (Spock)

What a difference; the data-driven power of the where: block makes this stuff a bit of a snap, and you can see in once place, at a glance, what's going on. IDEA even lines up all the pipe characters automatically (wow!). It's obvious how the tests execute, and easy to see how to add new tests for new cases. By comparison, the TestNG version looks like a blob of code ... it takes a bit more scrutiny to see exactly what it is testing and how.

In addition, the propertyMissing() trick means that any property (including public static fields) of Calendar is accessible without qualification, making things look even nicer. This is what they mean by writing an "executable specification", rather than just writing code.

I can't say this enough: using any other framework for testing Java or Groovy code would simply not be logical.

6 comments:

Tom Davies said...

Spock would only use a statically typed language.

Ivan Khalopik said...

Have a lot of fun doing the same thing in my project. Also I've been trying to use BDD principes instead of TDD. Before convertion tests looked as checks for the methods if they work correctly. Now they look like a documentation for some functionality. It's really cleaner and more understandable. Some guys also recommend me to try scalatest

P.S. More correct to compare TestNG tests with specified data provider.

Denis said...

You should use TestNG's @DataProvider for parameterized tests.

Howard Lewis Ship said...

Denis,

If you look at the T5 tests, you'll see extensive use of @DataProvider ... however not many people are aware of this functionality AND even when used, it is significantly inferior to what Spock provides.

Howard Lewis Ship said...

Tom,

I think you are missing a word there, eh? Do you mean "If only Spock would use ..."?

Sony Mathew said...

Can do similar in Java, create a class with a fluent interface then use it as follows:

cronTest
.expr(""0 15 10 * * ? 2005")
.calendar(2005,10,28,10,15,0)
.assert(true);

cronTest
.expr(""0 16 10 * * ? 2006")
.calendar(2010,10,28,10,15,0)
.assert(false);

and so on..