Chapter 8. Advanced where: Blocks
In Chapter 6, we saw how a Spock feature method can iterate over a data pipe defined in the where:
block. The examples in that chapter showed where:
blocks that used statically defined data and some using @Shared
fields as data providers. In this chapter, we look at a couple of less typical examples of data providers backed by external data: first a database and then a set of files output by a previous build step.
A Primary Key Verifier
I once implemented a test that confirmed that every table in our database schema had a primary key defined. A couple of times, we had forgotten to do this when adding a new table to the application and had experienced data integrity issues and performance degradation. We wanted a simple test that would catch this for us automatically; that is, whenever a new table was added, the test would check it automatically without the need for anyone to inform it about the existence of the new table.
This was a great opportunity to use a dynamic data pipe in a Spock where:
block. Recall that data pipes can use any Iterable
value as their source. In this case, we want a list of the names of every table in the database.
This example uses a @Shared
connection to a simple in-memory database and a SchemaBuilder
object that uses the connection to create the application’s database schema for us. The precise details of the schema itself aren’t important for this discussion. Assume that the SchemaBuilder.createSchema()
method creates ...
Get Spock: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.