This is the 16th day of my participation in the August Text Challenge.More challenges in August

Following up, Spock offers a number of concepts to help us quickly mock data and test results. If this is what a testing tool like Mockito can do, then the following feature is the killer: data-driven testing. It’s a good name, maybe not to the level of DDD, but at first it’s spooked.

Let’s start with an example:

class MathSpec extends Specification {
  def "maximum of two numbers"() {
    expect:
    // exercise math method for a few different inputs
    Math.max(1, 3) == 3
    Math.max(7, 4) == 7
    Math.max(0, 0) == 0
  }
}
Copy the code

This is a common test case mentioned above, but it’s not what Spock was looking for. If your method has 10 branches, you have to write at least 10 lines of method calls here. Too much code is bad, so here’s how Spock simplified it:

class MathSpec extends Specification {
  def "maximum of two numbers"() {
    expect:
    Math.max(a, b) == c

    where:
    a | b || c
    1 | 3 || 3
    7 | 4 || 7
    0 | 0 || 0
  }
}
Copy the code

Here you can write an infinite number of lines, the first line being the header, followed by the input and expected output. The specific format is as follows:

Input 1 | 2 | | output

Of course, you can write more input and output values corresponding to the first line. If you don’t want to use vertical lines, you can use semicolons, which may be slightly easier to type:

where: a ; b ;; c 1 ; 3;; 3 7; 4;; 7 0; 0;; 0Copy the code

For a twist, spock’s developers also provided a pipeline, with each variable’s value being a pipeline:

where:
a << [1, 7, 0]
b << [3, 4, 0]
c << [3, 7, 0]
Copy the code

If you’re still too bothered, Spock also offers a multivariable data pipeline:

@Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")

def "maximum of two numbers"() {
  expect:
  Math.max(a, b) == c

  where:
  [a, b, c] << sql.rows("select a, b, c from maxdata")
}
Copy the code

Read directly from the database and assign values directly to the corresponding variables. If you don’t want to list so many fields in SQL, spock doesn’t know what code practitioners need:

where:
[a, b, _, c] << sql.rows("select * from maxdata")
Copy the code

Undesired fields are simply ignored with _ on the left. If you still want to play with flowers, Spock also offers multi-field combinations:

where:
[a, [b, _, c]] << [
  ['a1', 'a2'].permutations(),
  [
    ['b1', 'd1', 'c1'],
    ['b2', 'd2', 'c2']
  ]
].combinations()
Copy the code

The effect of this combination is that the a field becomes a List like [‘ A1 ‘,’a2’], with the d in the middle ignored and b and C corresponding to b1, B2 and C1,c2, respectively. This is just a list of static data, and variable calculations are also supported:

where:
a | b
3 | a + 1
7 | a + 2
0 | a + 3
Copy the code

In such a framework system, can satisfy the vast majority of your imagination, to deal with different single test scenarios, single test seems not so boring.