Unit tests with Spock

This article has first appeared in Today Software Magazine

Why Spock?

When we first started working on our project at work, we searched for an easy-to-use tool that would help us write readable and concise tests. We chose Spock, because in combination with Groovy it provided exactly what we needed: readable BDD-style tests that we could write fast and change easily. Keep in mind that Spock can also be used in combination with Java, but has a great synergy with Groovy.

All the examples in this article are written in Groovy, but fear not! If you are a little bit familiar with Java, you will see that you can read Groovy code. Because our project’s tests examples would have been too complicated for a Spock introduction, I have chosen examples from a kata I coded to practice TDD.

1. Getting started

Adding Spock with Gradle is easy. This is my build.gradle file:

apply plugin: ‘groovy’

repositories {

    mavenCentral()

}

dependencies {

    testCompile(

            ‘junit:junit:4.12’,

            ‘org.codehaus.groovy:groovy-all:2.4.4’,

            ‘org.spockframework:spock-core:1.0-groovy-2.4’,

    )

    testRuntime(

// for spock reports

            ‘com.athaydes:spock-reports:1.2.7’

    )

}

Write the first falling test:

import spock.lang.Specification

class SeeIfItWorksSpec extends Specification {

   def “it should fail”() {

      expect:

      false

   }

}

The output is:

Condition not satisfied:

false

Afterwards, replace false with true. If the output is “Tests PASSED”, your setup is complete.

Read the complete article here.

More from the Blog

Leave a Comment

Your email address will not be published. Required fields are marked *

18
    18
    Your Cart
    Secure Coding
    Duration:

    4 days

    Format:

    Online Cohort Course

    Time:

    4h/day

    Start Date:

    25 Mar 2024

    Price: 781.00
    - +
    781.00
    Secure Coding
    Duration:

    4 days

    Format:

    Online Cohort Course

    Time:

    4h/day

    Start Date:

    22 April 2024

    Price: 781.00
    - +
    781.00
    4+ shoppers have bought this
    Applied Testing Strategy Workshop
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    17 June 2024

    Time:

    4h/day

    Price: 698.00
    - +
    698.00
    4+ shoppers have bought this
    Applied Testing Strategy Workshop
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    11 Nov 2024

    Time:

    4h/day

    Price: 698.00
    - +
    698.00
    Evolutionary Design
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    29 April 2024

    Time:

    4h/day

    Price: 631.00
    - +
    631.00
    Evolutionary Design
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    9 Dec 2024

    Time:

    4h/day

    Price: 631.00
    - +
    631.00
    Low-angle Photography of High-rise Building
    1+ shoppers have bought this
    From Developer to Architect
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    2 April 2024

    Time:

    4h/day

    Price: 748.00
    - +
    748.00
    Low-angle Photography of High-rise Building
    1+ shoppers have bought this
    From Developer to Architect
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    27 May 2024

    Time:

    4h/day

    Price: 748.00
    - +
    748.00
    Ferris Wheel at Night
    Serverless Architecture
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    29 April 2024

    Time:

    4h/day

    Price: 781.00
    - +
    781.00
    Ferris Wheel at Night
    Serverless Architecture
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    3 June 2024

    Time:

    4h/day

    Price: 781.00
    - +
    781.00
    Curtain Wall Building Under Clear Blue Sky
    Native Cloud Architecture
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    27 May 2024

    Time:

    4h/day

    Price: 781.00
    - +
    781.00
    Clean Code Learning Program
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    26 Feb 2024

    Time:

    4h/day

    Price: 681.00
    - +
    681.00
    Clean Code Learning Program
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    25 Mar 2024

    Time:

    4h/day

    Price: 681.00
    - +
    681.00
    Architecting Microservices
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    10 June 2024

    Time:

    4h/day

    Price: 748.00
    - +
    748.00
    Architecting Microservices
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    7 Oct 2024

    Time:

    4h/day

    Price: 748.00
    - +
    748.00
    Software Architecture Principles
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    15 April 2024

    Time:

    4h/day

    Price: 781.00
    - +
    781.00
    Software Architecture Principles
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    10 June 2024

    Time:

    4h/day

    Price: 781.00
    - +
    781.00
    Usable Software Design
    Duration:

    4 days

    Format:

    Online Cohort Course

    Start Date:

    20 May 2024

    Time:

    4h/day

    Price: 761.00
    - +
    761.00
      Apply Coupon
      Available Coupons
      individualcspo102022 Get 87.00 off
      Unavailable Coupons
      aniscppeurope2022 Get 20.00 off
      Scroll to Top