Java Reference
In-Depth Information
where:
[name,length] << [["Spock",5],["Kirk",4],["Scotty",6]]
}
}
The where block in the first test contains a data table. The column names ( name and
length ) are variables, which are referenced in the expect block. Groovy takes each
row of the table and evaluates the expect condition. It's an elegant system that's easy
to understand and quite powerful. While the data table is a powerful construct, in fact any
collection that Groovy knows how to iterate over works as well.
The second and third tests illustrate the same process but supply the data via collections.
The second test uses separate lists for the name and length values. This means that to
understand the test data youhave to match upthe collection indexes. Forexample, “Spock”
goeswith5,“Kirk”goeswith4,andsoon.Thethirdtestisabiteasier tovisualize, because
thedataisorganizedintoorderedpairs.Whichmechanismyouuse(datatable,setsofpairs,
individual collections, and so on) is purely a question of style.
Another interesting part of Spock is the @Unroll annotation. Without it, the name listed
in the test output would be the name of the test itself. With it, each row of the where block
creates a different name.
Figure 6.5 shows the results of executing this test in the Groovy and Grails Tool Suite
(which is just Eclipse plus lots of plugins) as a JUnit test. In addition to demonstrating that
Spock tests run with the existing JUnit infrastructure, the test also shows the difference in
output that results with the @Unroll annotation. The second and third tests use the name
of the method as their output. The first test, marked with @Unroll , shows up under “un-
rooted tests,” where each test gets its own unique name based on the test data.
Search WWH ::




Custom Search