Spock Web Console

subscribe to the feed Subscribe
sisg - 01 Spock Basics (via #spockwebconsole)
tweet this script Tweet

sisg - 01 Spock Basics

Published 7 months ago by Matthias Schulz with tags sisg Spock Basics
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
package basics

import spock.lang.Specification

import java.sql.SQLException

// Spock classes need to extend spock.lang.specification:
class SpockBasicsSpec extends Specification{


    // Init ///////////////////////////////////////////////////////////////////
    // Method names are defined

    // Execute before Class
    def setupSpec()
    {
        objects = new HashSet<String>()
    }

    // Execute after Class
    def cleanupSpec()
    {
        dropAllObjects()
    }

    // Execute before each test
    def setup()
    {
    }

    // Execute after each test
    def cleanup()
    {
    }


    // Tests ///////////////////////////////////////////////////////////////////

    // Spoc tests do contain have at least one of the following labels:
    // {given, expected, when, then, cleanup}
    def "Test 1: Create new object"() {
        given: "Setup the test; only once"
        String newObject = "One"

        expect: "Is everything as expected?; only once"
        countObjects() == 0
        !existsObject(newObject)

        when: "When I do these...; create as many 'when-then' pairs as needed"
        createObject(newObject)

        then: "...then I expect these results; belongs to 'when'"
        notThrown SQLException  // We explicitly do NOT expect a SQLException to be thrown; optional
        countObjects() == 1
        existsObject(newObject)

        cleanup: "Cleanup the test; only once"
        dropObject(newObject)
    }


    def "Test 2: Create existing object - simple exception handling"() {
        given:
        String newObject = "One"
        createObject(newObject)

        expect:
        countObjects() == 1
        existsObject(newObject)

        when: "Trying to create an existing object"
        createObject(newObject)

        then:
        thrown SQLException  // We expect a SQLException to be thrown; mandatory to be able to continue
        countObjects() == 1
        existsObject(newObject)

        cleanup:
        dropObject(newObject)
    }


    def "Test 3: Create existing object - detailed exception handling"() {
        given:
        String newObject = "One"
        createObject(newObject)

        expect:
        countObjects() == 1
        existsObject(newObject)

        when: "Trying to create an existing object"
        createObject(newObject)

        then:
        SQLException e = thrown SQLException // Catch the SQLException and assign it for further use
        e.errorCode == -20123 // Assert that the expected error code was thrown
        countObjects() == 1
        existsObject(newObject)

        cleanup:
        dropObject(newObject)
    }


    def "Test 4: Text assertion"() {
        given:
        String name = 'Tomas'

        expect:
        name == 'Tim'
    }


    def "Test 5: Number assertion"() {
        given:
        int age = 20

        when:
        age++

        then:
        age < 20
    }


    // Mockup System ///////////////////////////////////////////////////////////

    private static HashSet<String> objects

    private void createObject(final String objectName)
            throws SQLException
    {
        if (objects.contains(objectName)) {
            throw new SQLException("Object $objectName already exists!", "error", -20123)
        }
        objects.add(objectName)
    }

    private void dropObject(final String objectName)
    {
        objects.remove(objectName)
    }

    private void dropAllObjects()
    {
        objects.clear()
    }

    private boolean existsObject(final String objectName)
    {
        return objects.contains(objectName)
    }

    private int countObjects() {
        return objects.size()
    }
}