patternMinor
Comparing a list of messages with a list inside an object
Viewed 0 times
comparingwithmessageslistobjectinside
Problem
int i = 0
for ( expectedErrors in errors ) {
final List 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:
errors
[[error1],[error1,error2,error3]]Solution
Instead of this:
I believe you can write simply:
I don't really understand the point of this:
The only way this will be true is if this is true:
Which is a lot simpler.
Btw, this is not idiomatic in Groovy:
This is the idiomatic way:
(because non-empty collections are true)
final List 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 == expectedErrorsWhich is a lot simpler.
Btw, this is not idiomatic in Groovy:
assert [ ] == somethingThis is the idiomatic way:
assert !something(because non-empty collections are true)
Code Snippets
final List<String> actualErrors = new ArrayList<>()final actualErrors = []assert [ ] == ((actualErrors - expectedErrors) + (expectedErrors - actualErrors))assert actualErrors == expectedErrorsassert [ ] == somethingContext
StackExchange Code Review Q#73353, answer score: 3
Revisions (0)
No revisions yet.