Forum Discussion

markwhitton's avatar
markwhitton
New Contributor
7 years ago

SOAPUI 5.4.0 JSON Assertion of Double Values Ending .00

I have a script assertion which asserts double values for a JSON response.  These asserts have been working until the latest release of SOAPUI (5.4.0).  It would appear that JsonSlurper is truncating the value, removing .00 endings.  

 

Is this a known issue and is there a known workaround at this time?

 

My assert looks something like below...

 

def slurper = new JsonSlurper()

def json = slurper.parseText messageExchange.response.responseContent

assert json.accountBalance.value.toString() == "123.00"

  • I am running SoapUI 5.3.0 and I confirm that your script does not give an assertion error for me.

      

    assert GroovySystem.version == "2.1.7"
    def slurper = new JsonSlurper() def json = slurper.parseText '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}' assert json.accountBalance.value.toString() == "125755.00"

    It looks like JsonSlurper had significant work done to it in Groovy version 2.3 (http://groovy-lang.org/releasenotes/groovy-2.3.html). Then the below using the groovy console at https://groovyconsole.appspot.com/ 

     

    assert GroovySystem.version == "2.4.4"
    import groovy.json.JsonSlurper
    def slurper = new JsonSlurper()
    def json = slurper.parseText '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}'
    assert json.accountBalance.value.toString() == "125755.00"
    Assertion failed: 
    
    assert json.accountBalance.value.toString() == "125755.00"
           |    |              |     |          |
           |    |              |     125755     false
           |    |              125755
           |    [amountType:DEBIT, currency:CAD, value:125755]
           [accountBalance:[amountType:DEBIT, currency:CAD, value:125755]]
    
    	at Script1.run(Script1.groovy:5)

    I would suggest that the newer behaviour is probably still correct. The JSON data defines a number, not a string. In theory, all we should care about is

     

    assert json.accountBalance.value == 125755

    If the application you are testing really does care about the decimal places, that would be non standard. Otherwise the data should be a string.  But since it isn't, you might need to do some non standard parsing.

     

    def json = '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}'
    assert json =~ /"value"\s*:\s*125755.00/

     

  • JHunt's avatar
    JHunt
    Community Hero

    I am running SoapUI 5.3.0 and I confirm that your script does not give an assertion error for me.

      

    assert GroovySystem.version == "2.1.7"
    def slurper = new JsonSlurper() def json = slurper.parseText '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}' assert json.accountBalance.value.toString() == "125755.00"

    It looks like JsonSlurper had significant work done to it in Groovy version 2.3 (http://groovy-lang.org/releasenotes/groovy-2.3.html). Then the below using the groovy console at https://groovyconsole.appspot.com/ 

     

    assert GroovySystem.version == "2.4.4"
    import groovy.json.JsonSlurper
    def slurper = new JsonSlurper()
    def json = slurper.parseText '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}'
    assert json.accountBalance.value.toString() == "125755.00"
    Assertion failed: 
    
    assert json.accountBalance.value.toString() == "125755.00"
           |    |              |     |          |
           |    |              |     125755     false
           |    |              125755
           |    [amountType:DEBIT, currency:CAD, value:125755]
           [accountBalance:[amountType:DEBIT, currency:CAD, value:125755]]
    
    	at Script1.run(Script1.groovy:5)

    I would suggest that the newer behaviour is probably still correct. The JSON data defines a number, not a string. In theory, all we should care about is

     

    assert json.accountBalance.value == 125755

    If the application you are testing really does care about the decimal places, that would be non standard. Otherwise the data should be a string.  But since it isn't, you might need to do some non standard parsing.

     

    def json = '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}'
    assert json =~ /"value"\s*:\s*125755.00/

     

  • avidCoder's avatar
    avidCoder
    Super Contributor

    Already a ticket as raised for the issue. Please follow this link :- 

     

    ticket

     

    If not, raise a ticket again for the same. Else, Using XMLHolder, you could read the xml value and use text() method to get your after decimal printed. 

     

    def groovyUtil = new com.eviware.soapui.support.GroovyUtils(context)

    def holder = groovyUtil.getXmlHolder("Request 1#Response")

    def  decimalValue = holder.getNodeValue("//accountBalance/value/text()")

    assert decimalValue = "123.00"

     

  • JHunt's avatar
    JHunt
    Community Hero

    When I used your assertion script and {"accountBalance":123.00}, I got an error that I couldn't call getValue() on a BigDecimal. Do you really need .value.toString()?

     

    Here I don't use it:

     

    def slurper = new groovy.json.JsonSlurper()
    def json
    
    json = slurper.parseText '{"accountBalance":123}'
    assert json.accountBalance == 123
    assert json.accountBalance == 123.00
    assert json.accountBalance != "123"
    assert json.accountBalance != "123.00"
    
    json = slurper.parseText '{"accountBalance":123.00}'
    assert json.accountBalance == 123
    assert json.accountBalance == 123.00
    assert json.accountBalance != "123"
    assert json.accountBalance != "123.00"
    
    json = slurper.parseText '{"accountBalance":123.0}'
    assert json.accountBalance == 123
    assert json.accountBalance == 123.00
    assert json.accountBalance != "123"
    assert json.accountBalance != "123.00"
    
    json = slurper.parseText '{"accountBalance":"123"}'
    assert json.accountBalance != 123
    assert json.accountBalance != 123.00
    assert json.accountBalance == "123"
    assert json.accountBalance != "123.00"
    
    json = slurper.parseText '{"accountBalance":"123"}'
    assert json.accountBalance != 123
    assert json.accountBalance != 123.00
    assert json.accountBalance == "123"
    assert json.accountBalance != "123.00"

     

    If this doesn't solve it for you, then what is your JSON and what is the assertion failure message that you're getting?

     

     

    • markwhitton's avatar
      markwhitton
      New Contributor

      Thanks for your response.  I have included an example of the JSON below, along with the script assertion. 

       

      My requirement is to validate that there are 2 decimal places and it appears that the latest version (5.4.0) does not allow or support this any more; 5.3.0 worked just fine.

       

      import groovy.json.JsonSlurper

      def slurper = new JsonSlurper()
      def json = slurper.parseText '{"accountBalance":{"value":125755.00,"currency":"CAD","amountType":"DEBIT"}}'

      assert json.accountBalance.value.toString() == "125755.00"

       

      When running the above, I get the following error -

       

      assert json.accountBalance.value.toString() == "125755.00" | | | | | | | | 125755 false | | 125755 | [amountType:DEBIT, currency:CAD, value:125755] [accountBalance:[amountType:DEBIT, currency:CAD, value:125755]]