Do you write unit tests?

Test Easy and Prosper with Spock Framework

Tomche Delev

March 11, 2017



  • Why bother with unit testing?

  • How to enjoy writing unit tests?

  • Show me the code already!

What is unit testing?


A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward.

If the assumptions turn out to be wrong, the unit test has failed.

A "unit" is a method or function.

But why?

Why Most Unit Testing is Waste (2014)

What is the answer of the community?


Ok let's stop writing UT and see what happens.

Wait... we've already tried that, and we know the result pretty well

1. Dynamic languages

2. Documentation

3. Refactoring

4. Rely on human factor

5. On failure the system is equally broken

6. Fear of reusing

Did I fail to mention something?

UT cannot assert the correctness of your code.

But it will constructively assert its incorrectness.


Is Unit Testing worth the effort?

1. Big changes more quickly

2. When to stop coding

3. The tests and the code work together to achieve better code (TDD).

4. TDD helps with coding constipation.

5. Unit Tests help you really understand the design of the code you are working on.

6. Instant visual feedback.

7. Unit testing does not mean writing twice as much code.

8. Fowler: "Imperfect tests, run frequently, are much better than perfect tests that are never written at all"

Unit testing is a lot like going to the gym.

Then, after maybe one or two weeks, just as the soreness is going away, a Big Deadline begins approaching.

Are you doing something wrong?

The final answer

Unit Testing is usually worth the effort

BUT the amount of effort required isn't going to be the same for everybody

How to enjoy writing unit tests?

Spock Framework

Spock is:

  • Testing and specification framework for Java and Groovy applications

  • With beautiful and highly expressive specification language

  • Compatible with most IDEs, build tools, and continuous integration servers

  • Inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms

Why Spock?

  • Reduce the lines of test code

  • Make tests more readable

  • Turn tests into specifications

  • Make testing fun again!

Spock in the testing jungle

Use Case Conventional Tool(s) A Single Tool
Unit Testing JUnit
Mocking and Stubing EasyMock
Behaviour Driven Design (BDD) Cucamber

Comparison to JUnit Concepts

JUnit Spock
Test class Specification
Test Feature
Test method Feature method
@Before setup()
@After cleanup()
Assertion Condition
@Test(expected) Exception condition

Hello Spock

import spock.lang.Specification

class StarshipSpec extends Specification {

    def "science officer of the starship Enterprise is Spock"() {
        def starship = new Starship("Enterprise", "Spock")

        def actual = starship.getScienceOfficer()

        actual == "Spock"


State based testing (AAA)

  • Arrange

  • Act

  • Assert

BDD Style

  • Given

  • When

  • Then

Other blocks: expect, where

Basic Blocks Demo

Data Driven Testing Demo

Interaction Based Testing and Mocking Demo

Real world example Demo

Thank you!



This work was supported by