Live long and prosper - Spock

Spock is the new kid in town in the crowded world of testing frameworks.

Here is an article on Spock where the author tries to vent out years in frustration over JUnit in a very professionally demeanor.

What he was really trying to say is:

  1. The way JUnits are written is laborious
  2. With Spock you can do this:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Test_Factorial_Spock_Parameterized_Groovy extends Specification {
    @Unroll
    def 'iterative(#i) succeeds'() {
    expect:
    iterative(i) == r
    where:
    i | r
    0 | 1G
    1 | 1G
    7 | 5040G
    12 | 479001600G
    20 | 2432902008176640000G
    40 | 815915283247897734345611269596115894272000000000
    }
    @Unroll
    def 'iterative(#i) throws exception'() {
    when:
    iterative(i)
    then:
    thrown(IllegalArgumentException)
    where:
    i << [-1, -2, -5, -10, -20, -100]
    }
    }
    Mr. Spock

And I was sold.

The needs of the many outweigh the needs of the few ― Spock, Star Trek II: The Wrath of Khan

Good Spock Resourses:
http://jakubdziworski.github.io/java/groovy/spock/2016/05/14/spock-cheatsheet.html
http://codepipes.com/presentations/spock-vs-junit.pdf
https://github.com/spockframework/spock

Author

Sahil Ahuja

Posted on

2016-12-05

Updated on

2020-12-21

Licensed under

Comments