Reputation: 49
Currently when I use if else in a Groovy - spock when:, only the if is executed and the else is not. Is there any other way of implementing if-else inside spock tests? I tried switch case and encountered the same.
if (value == 'x' || 'y' || 'z') {
//execute below info
} else if (value == 'a') {
//execute below info
}
Upvotes: 2
Views: 3916
Reputation: 1469
Due to the groovy truth 'y' is treated as boolean true, that's why else is not executed.
Probably you tried to evaluate this:
if (value == 'x' || value == 'y' || value == 'y') {
//execute below info
} else if (value == 'z'){
//execute below info
}
But also you can try to modify the if-expression to:
if (value in ['x', 'y', 'y']) {...}
Upvotes: 1
Reputation: 10665
I am not sure if I have to make this a comment or an answer.
Your code under the else block is not executing because value == 'x' || 'y' || 'y'
is always true because the character literal 'y'
is always evaluated to true.
Non-empty Strings, GStrings and CharSequences are coerced to true.
Try this: if (value == 'x' || value == 'y' )
Upvotes: 1