concept datum table in category java

appears as: data tables, data table
Java Testing with Spock

This is an excerpt from Manning's book Java Testing with Spock.

These tests suffer from a lot of code duplication if code is handled carelessly. The test is always the same (for example, Is the username valid?), and only the input changes. Whereas JUnit has some facilities for this type of test (parameterized test), Spock takes a different turn, and offers a special DSL that allows you to embed data tables in Groovy source code. Data-driven tests are covered in chapter 5.

Data tables should be your bread and butter when writing Spock parameterized tests. They shine when all input and output parameters are known in advance and thus can be embedded directly in the source code.

Listing 5.12. Custom expressions in data tables

The MyInteger class is a simple enumeration that contains the first 10 integers. The IntegerFactory class is a trivial factory that converts strings to integers. The details of the code aren’t important; what you need to take away from this example is the flexibility of data tables. If you run this example, Spock will evaluate everything and present you with the final result, as shown in figure 5.11.

Figure 5.11. Spock will evaluate all expressions and statements so that they can be used as standard parameters. All statements from listing 5.12 finally resolve to integers.

I try to avoid this technique because I think it damages the readability of the test. I prefer to keep values in data tables simple. Using too many expressions in your data tables is a sign that you need to convert the tabular data into data pipes, as explained in the next section.