Forum Discussion

chathurad's avatar
chathurad
Contributor
6 years ago
Solved

Assert int value in an array

As on the image my responce data contains an array like this :300, 300, 300, 305, 310, 310, 310, 315, 320.

I need to verify whether the each valu is greater than or equal to 299.

But im getting this error.

Is there any way to resolve this?



  • int count = 0
    price.each {
        if (it > 299)
            count++
    }
    if (count > 0) assert false, "Not all price is greater than 299"

5 Replies

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor
    int count = 0
    price.each {
        if (it > 299)
            count++
    }
    if (count > 0) assert false, "Not all price is greater than 299"
    • chathurad's avatar
      chathurad
      Contributor

      What is it??? i cant understand what is the meaning on "it" in the context of groovy scripting


      aaronpliu wrote:
      int count = 0
      price.each {
          if (it > 299)
              count++
      }
      if (count > 0) assert false, "Not all price is greater than 299"


      .:robotmad:

      • nmrao's avatar
        nmrao
        Champion Level 3

        The piece of code is being "iterated" over the list and "it" is a reserved keyword and refers to the current element of the list during iteration.

         

         

  • nmrao's avatar
    nmrao
    Champion Level 3

    Goovy is a powerful yet simple to understand.

     

    The requirement can be achieved easily using below statement.

     

    //Define the list of numbers to be checked
    def listToBeChecked = [300, 300, 300, 305, 310, 310, 310, 315, 320]
    
    //Following will check every element against 299 and show error otherwise
    assert listToBeChecked.every{element -> element > 299 }, 'check failed'