gibb
gibb

Reputation: 1

Groovy map compare key from one test step and value of another test step and assert if it's true or false

I have groovy script like this:

def map = ['Response' : 'id']

def p1 = context.testCase.testSteps["TestStep_1"].properties
def p2 = context.testCase.testSteps["TestStep_2"].properties

def result = []

def assertPropertyValue = { p1key, p2key -> 
    def temp = p1[p1key].value == p2[p2key].value
    log.info("Comparing $p1key, and $p2key values respectively ${p1[p1key].value} == ${p2[p2key].value} ? $temp")
    temp
}
map.each { result << assertPropertyValue(it.key, it.value) }
assert result.each{it.value == true}, 'Comparison failed, check log'

My groovy script does not assert false despite value of TestScript_2 does not match with key from TestStep_1. I get the following log:

Fri Aug 12 17:48:16 CEST 2022:INFO:Comparing Response, and id values respectively {"code":"200","timestamp":"Fri Aug 12 15:12:45 UTC 2022","HttpStatus":"OK","id":"8154b2d1-4f83-4b2c-b100-5dab36b37ab6"} == 929c2a62-5c8a-4e85-bf65-776696503818 ? false

IDs does not match and I will expect my groovy test case to assert with "'Comparison failed, check log'". Do you have any idea why this happens?

Upvotes: 0

Views: 431

Answers (1)

albciff
albciff

Reputation: 18507

each on a List in groovy return a List so since your result object is not null neither empty, then your assert always returns true.

If you want to check that there is no false value inside result list, try with any:

assert !result.any{ it.value == false }, 'Comparision failed, check log'

Alternatively you can use find as @dagget suggest in his answer, however since find returns the object found or null it could be necessary an extra comparission to help assert to discern bettwen boolean object false and null. Using find:

assert result.find { it.value == false } == null, 'Comparision failed, check log'

Upvotes: 0

Related Questions