Make Your Customers Happy With Spock

Alican Akkus
3 min readMay 8, 2019

--

I am aware that the title is assertive. Completely my thoughts, but I think it’s correct.

Developers, probably feel happy writing a test with Spock. I recently told my pair(lemi) that I was very happy to write Spock tests. He replied, “I’m happy too.” 😀

When developers are happy, they build the right things right. Therefore customers will be happy.

God of Test

Spock And Groovy

We need tests to ensure that we build things are right. We write a unit, integration, functional, contract, load tests and so on.

Tests are hard to create and maintain. They need special attention. Spock eradicates the fear of test.

Spock eradicates the fear of test

Spock aims to be a more powerful, efficient and understandable alternative to the traditional Java test stack, by leveraging Groovy features. Groovy and Spock a gorgeous duo. Spock introduces new and expressive ways of testing our Java&Groovy applications, which simply aren’t possible in ordinary Java code.

Spock tests are self-explanatory and understandable. Test driven and behavior driven development with Spock are quite easy.

I recently wrote a test at my company. It was a very important money distribution calculation and contains a core domain logic. There were many different scenarios and variables in the test. I wrote it with a data table test.
If I hadn’t written with Spock, it would probably be too hard.

Let’s start coding;

Data Table Test

Essentially, data-driven testing is when we test the same behavior multiple times with different parameters(scenarios) and assertions.

expect-where notations is a readable and powerful feature of Spock. You can test many different scenarios in one test method. Let’s them more readable;

With @Unroll you can make your test more readable. You can use all parameters in method name with ‘#’ character.

Did I mention the Spock test was self-explanatory?

Syntactic Variations

Inputs and expected outputs can be separated with a double pipe symbol (||) to visually set them apart. With this, the code becomes:

BDD style

Spock uses the human-readable blocks such kind of given, when, then, etc.

Mocking

Mock objects are created with the Mock() method. Spock has a powerful mocking capability. Let’s create an order with mocking commission calculation.

Source code;

Test;

With this “orderCommissionCalculator.calculate(“CARD”, 100) >> 2.3" line we are calculate mock commission for order.

Interactions

Interaction is a regular method invocation. Let’s do it;

1 * orderCommissionCalculator.calculate("CARD", 100) >> 2.3 //exactly one call0 * _._ // don't allow any other interaction

Exception Conditions

Exception conditions are used to describe that a when block should throw an exception. They are defined using the thrown() method, passing along the expected exception type.

Conclusion

Testing has many benefits for products/developers/customers. Having testing in your project can save money for the long term. Reduces the bugs in the application lifecycle and improves product quality. Customers are happy to use a bug-free application. And this happiness can return to your company as money.

The benefits of tests are not just money. Developers do not attempt to refactor if the test does not exist in the code base. And this causes short-lived applications. Make the code refactorable by typing the test. In this way, we can deliver the value that we promise to customers.

Spock makes writing tests fun. You can fall in love with the code. And I really mean it. Spock is readable, self-explanatory, it does not require 3rd parties, easy to create, enforces developers to arrange their test in BDD like form, and it is more powerful than other alternatives.

Some examples can be found over the GitHub repo.

--

--