Do you write unit tests?

http://etc.ch/q46G

Test Easy and Prosper with Spock Framework

Tomche Delev

March 11, 2017

JavaSkop17'

Agenda

  • Why bother with unit testing?

  • How to enjoy writing unit tests?

  • Show me the code already!

What is unit testing?

Definition

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?

HN

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.

SO

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
TestNG
Spock
Mocking and Stubing EasyMock
jMock
Mockito
PowerMock
jMockit
Behaviour Driven Design (BDD) Cucamber
JBehave

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"() {
        given:
        def starship = new Starship("Enterprise", "Spock")

        when:
        def actual = starship.getScienceOfficer()

        then:
        actual == "Spock"
    }
}
    
        

Blocks

State based testing (AAA)

  • Arrange

  • Act

  • Assert

BDD Style

  • Given

  • When

  • Then

Other blocks: expect, where

Basic Blocks Demo

https://github.com/tdelev/javaskop17-spock

Data Driven Testing Demo

https://github.com/tdelev/javaskop17-spock

Interaction Based Testing and Mocking Demo

https://github.com/tdelev/javaskop17-spock

Real world example Demo

https://github.com/tdelev/spring-spock

Thank you!

Questions?

Acknowledgment

This work was supported by