int i = 0
for ( expectedErrors in errors ) {
    final List<String> actualErrors = new ArrayList<>()
    actualRequestMessages?.get(i++)?.errorData?.errors?.each {
        item -> actualErrors.add("${item.message}")
    assert [ ] == ((actualErrors - expectedErrors) + (expectedErrors - actualErrors)), [expectedErrors, actualErrors, "Expected Errors doesn't match actual"]

Currently, I am comparing actual error messages inside actualRequestMessages.errorData.errors with a list of expectedErrors as mentioned above. Is there a better way of doing it in Groovy?

Errors come from spock test in the form of a list of errors:


1 Answer 1


Instead of this:

final List<String> actualErrors = new ArrayList<>()

I believe you can write simply:

final actualErrors = []

I don't really understand the point of this:

assert [ ] == ((actualErrors - expectedErrors) + (expectedErrors - actualErrors))

The only way this will be true is if this is true:

assert actualErrors == expectedErrors

Which is a lot simpler.

Btw, this is not idiomatic in Groovy:

assert [ ] == something

This is the idiomatic way:

assert !something

(because non-empty collections are true)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.