Spock: feature methods

Spock is the test framework of Groovy and runs on the JUnit Platform. A specification is a class that extends spock.lang.Specification, and a feature method is a method with a name in quotes and a block of labels: given, when, then or expect.

The class under test

The example is one small class with two methods; the second one throws on a division by zero, which is what the third feature checks.

Calculator.groovy

class Calculator { int add(int a, int b) { a + b } int divide(int a, int b) { if (b == 0) { throw new IllegalArgumentException('division by zero') } a.intdiv(b) } }

The specification

CalculatorSpec.groovy

import spock.lang.Specification class CalculatorSpec extends Specification { def calculator = new Calculator() def 'adds two numbers'() { expect: calculator.add(2, 3) == 5 } def 'describes a calculation with given, when and then'() { given: 'a calculator' def calculator = new Calculator() when: 'two numbers are added' def result = calculator.add(20, 22) then: 'the sum is returned' result == 42 } def 'rejects a division by zero'() { when: calculator.divide(1, 0) then: def error = thrown(IllegalArgumentException) error.message == 'division by zero' } }

expect: holds a single condition, which is the short form of a test. given, when and then split a test into its setup, its action and its checks, and the strings after the labels are documentation for the reader and for the report. thrown catches the exception and returns it, so its message can be checked as well.

Run the specification

The compiler needs the Spock jars, and the launcher runs the class by name. The image keeps Spock 2.4 and its dependencies in /opt/spock, and the Groovy runtime next to it; the commands below are the ones the checker of this course runs, with the class path written out because the JUnit launcher does not expand a wildcard.

CP=/opt/spock/spock-core-2.4-groovy-5.0.jar:/opt/spock/byte-buddy-1.17.5.jar:/opt/spock/geantyref-1.3.16.jar:/opt/spock/hamcrest-3.0.jar:/opt/spock/objenesis-3.4.jar:/opt/groovy/lib/groovy-5.0.8.jar
groovyc -cp "$CP" -d out Calculator.groovy CalculatorSpec.groovy
junit execute --class-path "out:$CP" --select-class CalculatorSpec --disable-ansi-colors


Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
├─ JUnit Vintage ✔
└─ Spock ✔
   └─ CalculatorSpec ✔
      ├─ adds two numbers ✔
      ├─ describes a calculation with given, when and then ✔
      └─ rejects a division by zero ✔

Test run finished after <ms> ms
[ 5 containers found ]
[ 0 containers skipped ]
[ 5 containers started ]
[ 0 containers aborted ]
[ 5 containers successful ]
[ 0 containers failed ]
[ 3 tests found ]
[ 0 tests skipped ]
[ 3 tests started ]
[ 0 tests aborted ]
[ 3 tests successful ]
[ 0 tests failed ]

The tree lists the three features with a tick, and the counts below it say three tests ran and three passed. The time at the top changes between runs; everything else is the same on every machine.

Related

Автор статьи: Arthur Isaev