Forum Discussion

weissj's avatar
weissj
New Contributor
9 years ago
Solved

float automatically cast to int in assert so precision cannot be verified

I have an API endpoint that returns JSON.  One of the fields is a number with one digit to the right of the decimal point.   We need to verify that the value is correct with the correct precision.  

 

If the digit to the right of the decimal point is anything other than 0, the comparison works find using a JSON path match.  If the digit is a 0 however, the value seems to be cast to an int, either when the JSON is parsed or inside the assert. 

 

JSON snippet:

  "registerableInstances" : [ {
        "subjectCode" : "10",
        "subjectNumber" : "02",
        "percentContribution" : 15.0,
        "departmentCode" : "10",
        "sortKey" : "000.010.00.0000.0200.2.000",
        "primary" : false,
        "displayNumber" : "10.02"
      }...

Assert failure message:

-> Comparison failed for path [registerableInstances[0].percentContribution], expecting [15.0], actual was [15]

I also tried a regex match but this failed as well.

 

Does anyone know a way to force the value to be treated as a float or a string?  Or is there any other workaround?

 

Using Ready API 1.4.1.

 

Thanks.

  • You may do a script assertions something like below

     

    import net.sf.json.groovy.JsonSlurper
    def jsonText = '''{
    "registerableInstances" : {
            "subjectCode" : "10",
            "subjectNumber" : "02",
            "percentContribution" : 15.0,
            "departmentCode" : "10",
            "sortKey" : "000.010.00.0000.0200.2.000",
            "primary" : false,
            "displayNumber" : "10.02"
    }        
    }'''
    def jsonSlurper = new JsonSlurper()
    def ri= jsonSlurper.parseText(jsonText).registerableInstances
    def expectedContribution = 15.0
    def actualContribution = ri.percentContribution
    assert expectedContribution == actualContribution, "Expected value does not matches"

2 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    You may do a script assertions something like below

     

    import net.sf.json.groovy.JsonSlurper
    def jsonText = '''{
    "registerableInstances" : {
            "subjectCode" : "10",
            "subjectNumber" : "02",
            "percentContribution" : 15.0,
            "departmentCode" : "10",
            "sortKey" : "000.010.00.0000.0200.2.000",
            "primary" : false,
            "displayNumber" : "10.02"
    }        
    }'''
    def jsonSlurper = new JsonSlurper()
    def ri= jsonSlurper.parseText(jsonText).registerableInstances
    def expectedContribution = 15.0
    def actualContribution = ri.percentContribution
    assert expectedContribution == actualContribution, "Expected value does not matches"