Using Spock for testing code – getting started with syntax (1)

Getting started guide:
http://spockframework.org/spock/docs/1.0/spock_primer.html

Interesting things:

  • cool way to verify throwing exceptions:
    then: 
    EmptyStackException e = thrown() e.cause == null
  • instead of writing, “when-then”, just use “except”:
    expect:
    Math.max(1, 2) == 2
  • to parameterize test with different data, use “where” syntax:
    def "computing the maximum of two numbers"() {
    expect:
    Math.max(a, b) == c
    
    where:
    a << [5, 3]
    b << [1, 9]
    c << [5, 9]
    }
  • in helper method use “assert” keyword:
    assert pc.vendor == "Sunny"