Last Updated: February 25, 2016
· mariomelo

Groovy Spock "old" method

A good unit test is simple and have just a few lines. Spock has an awesome feature called "old" method that help us to keep our groovy tests even more clean and readable, take a look:

def "A test using the 'old' method from Spock"(){
    when: "I increment a number"

    then: "It must me greater than it was before"
        assert number > old(number)

    where: "we might have any kind of number"
        number << [497, 76.5, 0.01, -1]

The method gives us the value of a certain variable as it was at the beginning of the test.

Pretty cool, huh? :D