问题
I am having a little issue with my if condition in groovy.
Virtually I want to look at a property value I've set and ensure that is every instance on the response delivered from the JSON contains the same value as the property value, then it should equal a match. Now when I log the location_id
and location_id_request
, I am getting the values expected. However, in my if statement it seems to still state that that the location id does not match, making me believe that my if condition is incorrect. What do I need to change my if condition to, to output the correct message.
Below is the code I have with the log information underneath:
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(response)
def location_id = json.reviews.location_id
assert location_id != null
def location_id_request = messageExchange.modelItem.testStep.testCase.getPropertyValue("locationid")
assert location_id.every {it == location_id_request}
log.info location_id_request
log.info location_id
if (location_id == location_id_request)
log.info "Good News, location match!"
else
log.info "Test has failed, location do not match!"
Log information in correct order:
location_id_request:INFO:000000
location_id:INFO:[000000, 000000, 000000, 000000, 000000]
if condition output:INFO:Test has failed, location do not match!
回答1:
You compare a String
(Number
?) with List
- it won't work. Instead, try to check if given location_id
is present on location_id_request
List
:
if (location_id in location_id_request) { //... }
来源:https://stackoverflow.com/questions/41612455/if-condition-not-displaying-correct-message-after-output