Java Reference
In-Depth Information
The
tokenize
method takes a set of delimiters as arguments and divides the string at
those positions. The result is an
ArrayList
of words. That's interesting enough, but the
cool part is in the test that appends a new word to the list. In this case, the size of the list is
evaluated twice, once before the
when
block is executed and once afterward. The expres-
sion shows that the result afterward is equal to the result beforehand, plus one.
6.4.3. Data-driven specifications
Spock tests have one additional feature beyond what appears in other testing frameworks:
that Groovy can iterate over, then the test will run each entry through any supplied Boolean
conditions.
21
Shouldn't Data run on Android? (Yeah, that was particularly bad. Sorry.)
This is easier to show than to describe. Consider the test shown on the main page of the
Spock website, repeated in the next listing. It feeds names from a data table into
expect
,
using three different sources of data.
Listing 6.28. Data-driven Spock test
class HelloSpock extends spock.lang.Specification {
@Unroll
def "#name should be #length"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
'McCoy' | 5
}
def "check lengths using arrays"() {
expect: name.size() == length
where:
name << ["Spock","Kirk","Scotty"]
length << [5,4,6]
}
def "check lengths using pairs"() {
expect: name.size() == length