Hi,
Recently I wanted to make a soft condition verification (collect all the
failures and display them instead of fail on the first one [1]) in the
integration test in Spock and I haven't found any satisfactory solution.
I did a quick (and successful) PoC and it seems quite easy to add it to
Spock. Before polishing the implementation and making a PR I would like
to discuss the API first.
[1] -
https://github.com/joel-costigliola/assertj-core/wiki/New-and-noteworthy#soft-assertions
- Soft assertions in AssertJ
As each statement has to be executed separately (and potential
AssertionError be caught) I see no easy way to pass all the things as
one big Closure (and later split into chunks) and every condition has to
be passed in a separate Closure:
void assert(Closure<?>... conditions)`
def softly = new SoftConditions()
(...)
then:
softly.assert(
{ assert 1 },
{ assert foo == "Bar" }
)
SoftConditions could be (probably) also wrapped with PollingContitions
and similar.
I have seen in PollingConditions that assert keyword can be omitted with
the help of @ConditionBlock, so hopefully in SoftConditions it would be
possible as well.
To make basic usage easier there would a method `void
softly(Closure<?>... conditions)` in the Specification class which
creates SoftConditions and passes assertions to it:
then:
softly(
{ assert 1 },
{ assert foo == "Bar" }
)
I predict the misuse with one big Closure and two prevent that I have
two ideas:
1. Detect the situation when only one Closure is passed and throw very
verbose exception explaining that "there is no sense to use
SoftAssertions with just one statement and in case there are many
statements different statements should be passed in a separate Closures.
2. Changes method signature to `softly(Closure<?> condition1, Closure<?>
condition2, Closure<?>... conditions)`, but it will generate only a
warning (not a compilation error) and MissingMethodException at runtime
is much less meaningful.
What do you think about SoftAssertions in Spock in general and the
proposed API?
Marcin
--
http://blog.solidsoft.info/ - Working code is not enough