2

Here is my code after getting the response.

response.then().assertThat().body("documents[0].items[0].price.sales_tax_summary[0].rate",equalTo("0.0"));

here is the response log:

{
    "id": "quote-1",
    "documents": [
        {
            "id": "document-1",
            "items": [
                {
                    "id": "2",
                    "price": {
                        "amount_inclusive": 200000.5,
                        "amount_exclusive": 200000.5,
                        "total_tax": 0.0,
                        "tax_rate": 0.0,
                        "sales_tax_summary": [
                            {
                                "rate": 0.0,
                                "amount": 0.0
                            },
                            {
                                "rate": 0.0,
                                "amount": 0.0
                            }
                        ]
                    }
                }
            ],
            "shipping": {
                "id": "3",
                "price": {
                    "amount_inclusive": 15.0,
                    "amount_exclusive": 15.0,
                    "total_tax": 0.0,
                    "tax_rate": 0.0,
                    "sales_tax_summary": [
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        },
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        }
                    ]
                }
            },
            "handling": {
                "id": "4",
                "price": {
                    "amount_inclusive": 5.0,
                    "amount_exclusive": 5.0,
                    "total_tax": 0.0,
                    "tax_rate": 0.0,
                    "sales_tax_summary": [
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        },
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        }
                    ]
                }
            }
        }
    ]
}

here is the error message

java.lang.AssertionError: 1 expectation failed.
JSON path documents[0].items[0].price.sales_tax_summary[0].rate doesn't match.
Expected: 0.0
  Actual: 0.0

I have tried trimming, using containing substring, plugging 0.0 as double values instead of string as expected. but got the same error

avariant
  • 2,234
  • 5
  • 25
  • 33
Omar Sabha
  • 23
  • 5

2 Answers2

1

Comparing a String to a double is the issue.

Try this instead:

double expectedRate = 0.0;

double actualRate = response.then().extract().jsonPath().getDouble("documents[0].items[0].price.sales_tax_summary[0].rate");

assertTrue(expectedRate == actualRate);

That will make sure it compares as doubles instead of a string to a double and should get you the result you're looking for.

Gilles-Antoine Nys
  • 1,481
  • 16
  • 21
0

Your JSON returns a double value and you are comparing it with a string, that's the reason your Assertion is failing. Use equalTo() when comparing two strings. Use the following code:

response.then().assertThat().body("documents[0].items[0].price.sales_tax_summary[0].rate"== 0.0)

Deepak Attri
  • 102
  • 1
  • 5