Reputation: 4479
In Spock testing, are the when:
and then:
block labels (labels themselves, not the code/phases they separate) strictly necessary? Or are they technically just syntactic sugar, that can be omitted (while still having/keeping the code lines that would otherwise be separated by the labels)?
I'm not sure if the stimulus/response phases (respective to when/then) still execute the same, when omitting the labels?
(I am curious to ask this, for the purpose of possibly saving a few lines of code; that may be relatively trivial, but I appreciate if anyone who can answer would humor me. I am in a context where there are no non-technical or less-technical users reading or writing Spock tests - I respect other contexts where including labels aids in readability for the other non/less-technical users; that makes sense.)
For example (1), consider the below feature method:
def "a feature method with when/then labels"(){
// setup phase code
when:
// stimulus phase code
then:
// response phase code
where:
// data table
}
Would the below (example 2) edit (of above example, removing just the when/then labels) be still functionally equivalent to above?
def "a feature method withOUT when/then labels"(){
// setup phase code
// stimulus phase code
// response phase code
where:
// data table
}
I have at least partially convinced myself they are equivalent, with my own before/after testing (of an edit like above) that "appears" to me to function the same (per its results) - but I am not sure if the respective stimulus/response phases still execute the same, when omitting the labels. Despite the identical results from my own testing I mentioned, I'm not sure if there may be differentiating side-effects that may not be so immediately apparent from the results themselves.
I wouldn't be surprised if example 2 above constitutes everything above its where
block, to be in its implicit given block. But even if so, I'm not sure if that would make a difference, per the two examples being functionally equivalent or not.
Here's a few when/then block pair facts (for general context for others):
when:
or just then:
(for a given pair of stimulus/response phases)Upvotes: 1
Views: 1211
Reputation: 67317
I suggest you read the Spock manual. The blocks carry semantics and based on how you structure them the Groovy compiler will apply AST (abstract syntax tree) transformations upon your Spock specifications.
Why would you use Spock and try to cast away the very semantics delivered to you by this tool? If you don't like the behaviour-driven structure of Spock tests, just use pure Java or Groovy tests and the tool of your choice, such as JUnit or TestNG. But then if you wish to use mocks you need additional mock tools which, ironically, often re-introduce a behaviour-driven when-then or given-expect type of structure.
Minimally, as you noticed, you need at least one Spock-specific block in order for the test to be identified as a feature method. While you could omit given|setup
and just use expect
instead of when-then
, this is only applicable to cases in which it makes sense. Try not to force everything into that structure but also use the other block types in order to clearly describe each feature in your specification. Technically, a then|expect
block brings you the semantic of automatic assertions for all specified conditions (without having to use assert
or a JUnit method like assertEquals
). A where
block brings you easy feature method parametrisation. An optional cleanup
block helps you to close and/or clean up test resources cleanly without having to manually use try-finally
constructs.
I suggest you learn how to use Spock as a tool correctly and enjoy its features instead of somehow trying to work around it, which would make using it rather pointless.
Upvotes: 4